home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 4 / MacFormat n. 4 (Spain) / MacFormat 4.bin / La ciudad del ShareWare / Desarrollo / OutOfPhase1.1 Source / OutOfPhase Folder / SampleWindow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-28  |  107.5 KB  |  3,497 lines

  1. /* SampleWindow.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "SampleWindow.h"
  31. #include "Memory.h"
  32. #include "MainWindowStuff.h"
  33. #include "CodeCenter.h"
  34. #include "SampleObject.h"
  35. #include "SampleView.h"
  36. #include "TextEdit.h"
  37. #include "WindowDispatcher.h"
  38. #include "Alert.h"
  39. #include "GrowIcon.h"
  40. #include "SampleTestPlay.h"
  41. #include "DataMunging.h"
  42. #include "Main.h"
  43. #include "Numbers.h"
  44. #include "PcodeStack.h"
  45. #include "PcodeSystem.h"
  46. #include "Scroll.h"
  47. #include "SampleList.h"
  48. #include "EditImages.h"
  49. #include "IconButton.h"
  50. #include "SampleStorageActual.h"
  51. #include "SampleStorageDisplay.h"
  52. #include "FindDialog.h"
  53. #include "GlobalWindowMenuList.h"
  54. #include "CompilerRoot.h"
  55. #include "FunctionCode.h"
  56. #include "ExportWAVSample.h"
  57. #include "ExportRAWSample.h"
  58. #include "ExportAIFFSample.h"
  59.  
  60.  
  61. #define OVERLINEHEIGHT (13)
  62.  
  63. #define ZOOMFACTOR (2) /* zoom doubles / halves the resolution */
  64.  
  65. #define WINDOWXSIZE (512 - 4 - 4)
  66. #define WINDOWYSIZE (342 - 20 - 20 - 4 - 4)
  67.  
  68. #define EXPRESSIONEDITX (-1)
  69. #define EXPRESSIONEDITWIDTH(WindowWidth) ((WindowWidth) + 2)
  70. #define EXPRESSIONEDITHEIGHT (70)
  71. #define EXPRESSIONEDITY(WindowHeight) ((WindowHeight) - EXPRESSIONEDITHEIGHT + 1)
  72.  
  73. #define SAMPLEEDITX (-1)
  74. #define SAMPLEEDITY (41)
  75. #define SAMPLEEDITWIDTH(WindowWidth) ((WindowWidth) + 2)
  76. #define SAMPLEEDITHEIGHT(WindowHeight) ((WindowHeight) - SAMPLEEDITY - 70\
  77.                     - EXPRESSIONEDITHEIGHT)
  78.  
  79. #define NAMEEDITX (80)
  80. #define NAMEEDITY (1)
  81. #define NAMEEDITWIDTH (80)
  82. #define NAMEEDITHEIGHT (19)
  83.  
  84. #define SAMPLINGRATEEDITX (80)
  85. #define SAMPLINGRATEEDITY (21)
  86. #define SAMPLINGRATEEDITWIDTH (80)
  87. #define SAMPLINGRATEEDITHEIGHT (19)
  88.  
  89. #define ORIGINEDITX (240)
  90. #define ORIGINEDITY (1)
  91. #define ORIGINEDITWIDTH (80)
  92. #define ORIGINEDITHEIGHT (19)
  93.  
  94. #define BASEFREQEDITX (240)
  95. #define BASEFREQEDITY (21)
  96. #define BASEFREQEDITWIDTH (80)
  97. #define BASEFREQEDITHEIGHT (19)
  98.  
  99. #define LOOPSTARTEDITX (400)
  100. #define LOOPSTARTEDITY (1)
  101. #define LOOPSTARTEDITWIDTH (80)
  102. #define LOOPSTARTEDITHEIGHT (19)
  103.  
  104. #define LOOPENDEDITX (400)
  105. #define LOOPENDEDITY (21)
  106. #define LOOPENDEDITWIDTH (80)
  107. #define LOOPENDEDITHEIGHT (19)
  108.  
  109. #define SELECTSTARTEDITX (160)
  110. #define SELECTSTARTEDITY(WinHeight) ((WinHeight) - 19 - 19 - 19 - 3\
  111.                     - EXPRESSIONEDITHEIGHT)
  112. #define SELECTSTARTEDITWIDTH (80)
  113. #define SELECTSTARTEDITHEIGHT (19)
  114.  
  115. #define SELECTENDEDITX (160)
  116. #define SELECTENDEDITY(WinHeight) ((WinHeight) - 19 - 19 - 2 - EXPRESSIONEDITHEIGHT)
  117. #define SELECTENDEDITWIDTH (80)
  118. #define SELECTENDEDITHEIGHT (19)
  119.  
  120. #define SCALINGEDITX (330)
  121. #define SCALINGEDITY(WinHeight) ((WinHeight) - 19 - 19 - 19 - 3 - EXPRESSIONEDITHEIGHT)
  122. #define SCALINGEDITWIDTH (80)
  123. #define SCALINGEDITHEIGHT (19)
  124.  
  125. #define MOUSEPOSEDITX (160)
  126. #define MOUSEPOSEDITY(WindowHeight) ((WindowHeight) - 19 - 1 - EXPRESSIONEDITHEIGHT)
  127. #define MOUSEPOSEDITWIDTH (80)
  128. #define MOUSEPOSEDITHEIGHT (19)
  129.  
  130. #define BITS8X (1)
  131. #define BITS8Y(WinHeight) ((WinHeight) - 33 - EXPRESSIONEDITHEIGHT)
  132.  
  133. #define BITS16X (BITS8X + 33)
  134. #define BITS16Y(WinHeight) (BITS8Y(WinHeight))
  135.  
  136. #define MONOX (1)
  137. #define MONOY(WinHeight) ((WinHeight) - 33 - 33 - 3 - EXPRESSIONEDITHEIGHT)
  138.  
  139. #define STEREOX (MONOX + 33)
  140. #define STEREOY(WinHeight) (MONOY(WinHeight))
  141.  
  142. #define PLAYX (410 + 2)
  143. #define PLAYY(WinHeight) ((WinHeight) - 33 - EXPRESSIONEDITHEIGHT)
  144.  
  145. #define ZOOMINX (410 + 2)
  146. #define ZOOMINY(WinHeight) ((WinHeight) - 33 - 33 - 3 - EXPRESSIONEDITHEIGHT)
  147.  
  148. #define ZOOMOUTX (ZOOMINX + 33)
  149. #define ZOOMOUTY(WinHeight) (ZOOMINY(WinHeight))
  150.  
  151. #define SAMPLENAMEX (3)
  152. #define SAMPLENAMEY (4)
  153.  
  154. #define SAMPLERATEX (3)
  155. #define SAMPLERATEY (23)
  156.  
  157. #define ORIGINX (170)
  158. #define ORIGINY (4)
  159.  
  160. #define BASEFREQX (170)
  161. #define BASEFREQY (23)
  162.  
  163. #define LOOPSTARTX (330)
  164. #define LOOPSTARTY (4)
  165.  
  166. #define LOOPENDX (330)
  167. #define LOOPENDY (23)
  168.  
  169. #define SELECTSTARTX (76)
  170. #define SELECTSTARTY(WinHeight) ((WinHeight) - 19 - 19 - 19 - 3 + 3\
  171.                     - EXPRESSIONEDITHEIGHT)
  172.  
  173. #define SELECTENDX (76)
  174. #define SELECTENDY(WinHeight) ((WinHeight) - 19 - 19 - 2 + 3 - EXPRESSIONEDITHEIGHT)
  175.  
  176. #define MOUSELOCX (76)
  177. #define MOUSELOCY(WinHeight) ((WinHeight) - 19 - 1 + 3 - EXPRESSIONEDITHEIGHT)
  178.  
  179. #define SCALINGX (250)
  180. #define SCALINGY(WinHeight) ((WinHeight) - 19 - 19 - 19 - 3 + 3 - EXPRESSIONEDITHEIGHT)
  181.  
  182.  
  183. struct SampleWindowRec
  184.     {
  185.         MainWindowRec*            MainWindow;
  186.         CodeCenterRec*            CodeCenter;
  187.         SampleObjectRec*        SampleObject;
  188.         SampleListRec*            SampleList;
  189.  
  190.         WinType*                        ScreenID;
  191.         SampleViewRec*            SampleView;
  192.         TextEditRec*                NameEdit;
  193.         TextEditRec*                SampleExpressionEdit;
  194.         TextEditRec*                SamplingRateEdit;
  195.         TextEditRec*                OriginEdit;
  196.         TextEditRec*                NaturalFrequencyEdit;
  197.         TextEditRec*                LoopStartEdit;
  198.         TextEditRec*                LoopEndEdit;
  199.         SampleLoops                    EditingWhichLoop;
  200.         TextEditRec*                SelectionStartEdit;
  201.         TextEditRec*                SelectionEndEdit;
  202.         TextEditRec*                ScalingEdit;
  203.         TextEditRec*                ActiveTextEdit;
  204.         TextEditRec*                MousePositionEdit;
  205.         ScrollRec*                    Scrollbar;
  206.         IconButtonRec*            Bits8Button;
  207.         IconButtonRec*            Bits16Button;
  208.         IconButtonRec*            MonoButton;
  209.         IconButtonRec*            StereoButton;
  210.         IconButtonRec*            ZoomInButton;
  211.         IconButtonRec*            ZoomOutButton;
  212.         IconButtonRec*            PlayButton;
  213.         GenericWindowRec*        MyGenericWindow; /* how the window event dispatcher knows us */
  214.         MenuItemType*                MyMenuItem;
  215.         long                                UndoOrigin;
  216.         long                                UndoLoopStart1;
  217.         long                                UndoLoopStart2;
  218.         long                                UndoLoopStart3;
  219.         long                                UndoLoopEnd1;
  220.         long                                UndoLoopEnd2;
  221.         long                                UndoLoopEnd3;
  222.     };
  223.  
  224.  
  225. /* create a new sample editing window.  the function gets ownership of SampleStorage */
  226. SampleWindowRec*        NewSampleWindow(struct MainWindowRec* MainWindow,
  227.                                             struct SampleObjectRec* SampleObject,
  228.                                             struct CodeCenterRec* CodeCenter,
  229.                                             struct SampleStorageActualRec* SampleStorage,
  230.                                             struct SampleListRec* SampleList,
  231.                                             OrdType WinX, OrdType WinY, OrdType WinWidth, OrdType WinHeight)
  232.     {
  233.         SampleWindowRec*    Window;
  234.         char*                            StringTemp;
  235.         NumChannelsType        NumChannels;
  236.         SampleStorageDisplayRec*    SampleCopy;
  237.         long                            Limit;
  238.         long                            Scan;
  239.  
  240.         /* deal with window placement */
  241.         if ((WinWidth < 100) || (WinHeight < 100) || ((eOptionKey & CheckModifiers()) != 0))
  242.             {
  243.                 WinX = 1 + WindowOtherEdgeWidths(eDocumentWindow);
  244.                 WinY = 1 + WindowTitleBarHeight(eDocumentWindow);
  245.                 WinWidth = WINDOWXSIZE;
  246.                 WinHeight = WINDOWYSIZE;
  247.             }
  248.         MakeWindowFitOnScreen(&WinX,&WinY,&WinWidth,&WinHeight);
  249.  
  250.         Window = (SampleWindowRec*)AllocPtrCanFail(
  251.             sizeof(SampleWindowRec),"SampleWindowRec");
  252.         if (Window == NIL)
  253.             {
  254.              FailurePoint1:
  255.                 AlertHalt("There is not enough memory available to "
  256.                     "display the sample editor.",NIL);
  257.                 return NIL;
  258.             }
  259.         Window->MainWindow = MainWindow;
  260.         Window->SampleObject = SampleObject;
  261.         Window->CodeCenter = CodeCenter;
  262.         Window->SampleList = SampleList;
  263.  
  264.         Window->ScreenID = MakeNewWindow(eDocumentWindow,eWindowClosable,
  265.             eWindowZoomable,eWindowResizable,WinX,WinY,WinWidth,WinHeight,
  266.             (void (*)(void*))&SampleWindowUpdator,Window);
  267.         if (Window->ScreenID == 0)
  268.             {
  269.              FailurePoint2:
  270.                 ReleasePtr((char*)Window);
  271.                 goto FailurePoint1;
  272.             }
  273.  
  274.         /* create sample editor */
  275.         NumChannels = GetSampleStorageActualNumChannels(SampleStorage);
  276.         SampleCopy = NewSampleStorageDisplayEmpty(
  277.             GetSampleStorageActualNumBits(SampleStorage),NumChannels);
  278.         if (SampleCopy == NIL)
  279.             {
  280.              FailurePoint3:
  281.                 KillWindow(Window->ScreenID);
  282.                 goto FailurePoint2;
  283.             }
  284.         Limit = GetSampleStorageActualNumFrames(SampleStorage);
  285.         if (!InsertSampleStorageDisplayArea(SampleCopy,0,Limit))
  286.             {
  287.                 goto FailurePoint3;
  288.             }
  289.         switch (NumChannels)
  290.             {
  291.                 default:
  292.                     EXECUTE(PRERR(ForceAbort,"NewSampleWindow:  bad num bits"));
  293.                     break;
  294.                 case eSampleMono:
  295.                     for (Scan = 0; Scan < Limit; Scan += 1)
  296.                         {
  297.                             SetSampleStorageDisplayValue(SampleCopy,Scan,eMonoChannel,
  298.                                 GetSampleStorageActualValue(SampleStorage,Scan,eMonoChannel));
  299.                         }
  300.                     break;
  301.                 case eSampleStereo:
  302.                     for (Scan = 0; Scan < Limit; Scan += 1)
  303.                         {
  304.                             SetSampleStorageDisplayValue(SampleCopy,Scan,eLeftChannel,
  305.                                 GetSampleStorageActualValue(SampleStorage,Scan,eLeftChannel));
  306.                             SetSampleStorageDisplayValue(SampleCopy,Scan,eRightChannel,
  307.                                 GetSampleStorageActualValue(SampleStorage,Scan,eRightChannel));
  308.                         }
  309.                     break;
  310.             }
  311.         Window->SampleView = NewSampleView(Window->ScreenID,SAMPLEEDITX,
  312.             SAMPLEEDITY + (3 * OVERLINEHEIGHT),SAMPLEEDITWIDTH(WinWidth),
  313.             SAMPLEEDITHEIGHT(WinHeight) - 15 - (3 * OVERLINEHEIGHT),SampleCopy);
  314.         if (Window->SampleView == NIL)
  315.             {
  316.                 /* this is not a true exception point because SampleCopy is only */
  317.                 /* valid & needs to be dumped if this call fails, otherwise no. */
  318.                 DisposeSampleStorageDisplay(SampleCopy);
  319.                 goto FailurePoint3;
  320.             }
  321.         SampleViewHasBeenSaved(Window->SampleView);
  322.  
  323.         /* create name edit */
  324.         Window->NameEdit = NewTextEdit(Window->ScreenID,eTENoScrollBars,
  325.             GetScreenFont(),9,NAMEEDITX,NAMEEDITY,NAMEEDITWIDTH,NAMEEDITHEIGHT);
  326.         if (Window->NameEdit == NIL)
  327.             {
  328.              FailurePoint4:
  329.                 DisposeSampleView(Window->SampleView);
  330.                 goto FailurePoint3;
  331.             }
  332.         /* install new text in name edit */
  333.         StringTemp = SampleObjectGetNameCopy(SampleObject);
  334.         if (StringTemp == NIL)
  335.             {
  336.              FailurePoint5:
  337.                 DisposeTextEdit(Window->NameEdit);
  338.                 goto FailurePoint4;
  339.             }
  340.         TextEditNewRawData(Window->NameEdit,StringTemp,"\x0a");
  341.         ReleasePtr(StringTemp);
  342.         TextEditHasBeenSaved(Window->NameEdit);
  343.  
  344.         /* create sampling rate edit */
  345.         Window->SamplingRateEdit = NewTextEdit(Window->ScreenID,eTENoScrollBars,
  346.             GetScreenFont(),9,SAMPLINGRATEEDITX,SAMPLINGRATEEDITY,
  347.             SAMPLINGRATEEDITWIDTH,SAMPLINGRATEEDITHEIGHT);
  348.         if (Window->SamplingRateEdit == NIL)
  349.             {
  350.              FailurePoint6:
  351.                 goto FailurePoint5;
  352.             }
  353.  
  354.         /* create origin edit */
  355.         Window->OriginEdit = NewTextEdit(Window->ScreenID,eTENoScrollBars,
  356.             GetScreenFont(),9,ORIGINEDITX,ORIGINEDITY,ORIGINEDITWIDTH,ORIGINEDITHEIGHT);
  357.         if (Window->OriginEdit == NIL)
  358.             {
  359.              FailurePoint7:
  360.                 DisposeTextEdit(Window->SamplingRateEdit);
  361.                 goto FailurePoint6;
  362.             }
  363.  
  364.         /* create base frequency edit */
  365.         Window->NaturalFrequencyEdit = NewTextEdit(Window->ScreenID,eTENoScrollBars,
  366.             GetScreenFont(),9,BASEFREQEDITX,BASEFREQEDITY,BASEFREQEDITWIDTH,
  367.             BASEFREQEDITHEIGHT);
  368.         if (Window->NaturalFrequencyEdit == NIL)
  369.             {
  370.              FailurePoint8:
  371.                 DisposeTextEdit(Window->OriginEdit);
  372.                 goto FailurePoint7;
  373.             }
  374.  
  375.         /* create loop start edit */
  376.         Window->LoopStartEdit = NewTextEdit(Window->ScreenID,eTENoScrollBars,
  377.             GetScreenFont(),9,LOOPSTARTEDITX,LOOPSTARTEDITY,LOOPSTARTEDITWIDTH,
  378.             LOOPSTARTEDITHEIGHT);
  379.         if (Window->LoopStartEdit == NIL)
  380.             {
  381.              FailurePoint9:
  382.                 DisposeTextEdit(Window->NaturalFrequencyEdit);
  383.                 goto FailurePoint8;
  384.             }
  385.  
  386.         /* create loop end edit */
  387.         Window->LoopEndEdit = NewTextEdit(Window->ScreenID,eTENoScrollBars,
  388.             GetScreenFont(),9,LOOPENDEDITX,LOOPENDEDITY,LOOPENDEDITWIDTH,LOOPENDEDITHEIGHT);
  389.         if (Window->LoopEndEdit == NIL)
  390.             {
  391.              FailurePoint10:
  392.                 DisposeTextEdit(Window->LoopStartEdit);
  393.                 goto FailurePoint9;
  394.             }
  395.  
  396.         Window->EditingWhichLoop = eSampleLoop1;
  397.  
  398.         /* create selection start edit */
  399.         Window->SelectionStartEdit = NewTextEdit(Window->ScreenID,eTENoScrollBars,
  400.             GetScreenFont(),9,SELECTSTARTEDITX,SELECTSTARTEDITY(WinHeight),
  401.             SELECTSTARTEDITWIDTH,SELECTSTARTEDITHEIGHT);
  402.         if (Window->SelectionStartEdit == NIL)
  403.             {
  404.              FailurePoint11:
  405.                 DisposeTextEdit(Window->LoopEndEdit);
  406.                 goto FailurePoint10;
  407.             }
  408.  
  409.         /* create selection end edit */
  410.         Window->SelectionEndEdit = NewTextEdit(Window->ScreenID,eTENoScrollBars,
  411.             GetScreenFont(),9,SELECTENDEDITX,SELECTENDEDITY(WinHeight),
  412.             SELECTENDEDITWIDTH,SELECTENDEDITHEIGHT);
  413.         if (Window->SelectionEndEdit == NIL)
  414.             {
  415.              FailurePoint12:
  416.                 DisposeTextEdit(Window->SelectionStartEdit);
  417.                 goto FailurePoint11;
  418.             }
  419.  
  420.         /* create scaling edit */
  421.         Window->ScalingEdit = NewTextEdit(Window->ScreenID,eTENoScrollBars,
  422.             GetScreenFont(),9,SCALINGEDITX,SCALINGEDITY(WinHeight),
  423.             SCALINGEDITWIDTH,SCALINGEDITHEIGHT);
  424.         if (Window->ScalingEdit == NIL)
  425.             {
  426.              FailurePoint13:
  427.                 DisposeTextEdit(Window->SelectionEndEdit);
  428.                 goto FailurePoint12;
  429.             }
  430.  
  431.         /* create place to show mouse position */
  432.         Window->MousePositionEdit = NewTextEdit(Window->ScreenID,eTENoScrollBars,
  433.             GetScreenFont(),9,MOUSEPOSEDITX,MOUSEPOSEDITY(WinHeight),
  434.             MOUSEPOSEDITWIDTH,MOUSEPOSEDITHEIGHT);
  435.         if (Window->MousePositionEdit == NIL)
  436.             {
  437.              FailurePoint14:
  438.                 DisposeTextEdit(Window->ScalingEdit);
  439.                 goto FailurePoint13;
  440.             }
  441.  
  442.         /* expression editor */
  443.         Window->SampleExpressionEdit = NewTextEdit(Window->ScreenID,
  444.             (TEScrollType)(eTEVScrollBar | eTEHScrollBar),GetMonospacedFont(),9,
  445.             EXPRESSIONEDITX,EXPRESSIONEDITY(WinHeight),EXPRESSIONEDITWIDTH(WinWidth),
  446.             EXPRESSIONEDITHEIGHT);
  447.         if (Window->SampleExpressionEdit == NIL)
  448.             {
  449.              FailurePoint15:
  450.                 DisposeTextEdit(Window->MousePositionEdit);
  451.                 goto FailurePoint14;
  452.             }
  453.         StringTemp = SampleObjectGetFormulaCopy(SampleObject);
  454.         if (StringTemp == NIL)
  455.             {
  456.              FailurePoint16:
  457.                 DisposeTextEdit(Window->SampleExpressionEdit);
  458.                 goto FailurePoint15;
  459.             }
  460.         SetTextEditAutoIndent(Window->SampleExpressionEdit,True);
  461.         SetTextEditTabSize(Window->SampleExpressionEdit,MainWindowGetTabSize(MainWindow));
  462.         TextEditNewRawData(Window->SampleExpressionEdit,StringTemp,"\x0a");
  463.         ReleasePtr(StringTemp);
  464.  
  465.         Window->Scrollbar = NewScrollBar(Window->ScreenID,eHScrollBar,SAMPLEEDITX,
  466.             SAMPLEEDITY + SAMPLEEDITHEIGHT(WinHeight) - 16,SAMPLEEDITWIDTH(WinWidth));
  467.         if (Window->Scrollbar == NIL)
  468.             {
  469.              FailurePoint17:
  470.                 goto FailurePoint16;
  471.             }
  472.  
  473.         Window->Bits8Button = NewIconButtonPreparedBitmaps(Window->ScreenID,BITS8X,
  474.             BITS8Y(WinHeight),32,32,Bits8Unselected,Bits8MouseDown,
  475.             Bits8Selected,Bits8Selected,eIconRadioMode);
  476.         if (Window->Bits8Button == NIL)
  477.             {
  478.              FailurePoint18:
  479.                 DisposeScrollBar(Window->Scrollbar);
  480.                 goto FailurePoint17;
  481.             }
  482.  
  483.         Window->Bits16Button = NewIconButtonPreparedBitmaps(Window->ScreenID,BITS16X,
  484.             BITS16Y(WinHeight),32,32,Bits16Unselected,Bits16MouseDown,
  485.             Bits16Selected,Bits16Selected,eIconRadioMode);
  486.         if (Window->Bits16Button == NIL)
  487.             {
  488.              FailurePoint19:
  489.                 DisposeIconButton(Window->Bits8Button);
  490.                 goto FailurePoint18;
  491.             }
  492.  
  493.         switch (SampleObjectGetNumBits(SampleObject))
  494.             {
  495.                 case eSample8bit:
  496.                     SetIconButtonState(Window->Bits8Button,True);
  497.                     break;
  498.                 case eSample16bit:
  499.                     SetIconButtonState(Window->Bits16Button,True);
  500.                     break;
  501.                 default:
  502.                     EXECUTE(PRERR(ForceAbort,"NewSampleWindow:  bad number of bits value"));
  503.                     break;
  504.             }
  505.  
  506.         Window->MonoButton = NewIconButtonPreparedBitmaps(Window->ScreenID,MONOX,
  507.             MONOY(WinHeight),32,32,MonoUnselected,MonoMouseDown,
  508.             MonoSelected,MonoSelected,eIconRadioMode);
  509.         if (Window->MonoButton == NIL)
  510.             {
  511.              FailurePoint20:
  512.                 DisposeIconButton(Window->Bits16Button);
  513.                 goto FailurePoint19;
  514.             }
  515.  
  516.         Window->StereoButton = NewIconButtonPreparedBitmaps(Window->ScreenID,STEREOX,
  517.             STEREOY(WinHeight),32,32,StereoUnselected,StereoMouseDown,
  518.             StereoSelected,StereoSelected,eIconRadioMode);
  519.         if (Window->StereoButton == NIL)
  520.             {
  521.              FailurePoint21:
  522.                 DisposeIconButton(Window->MonoButton);
  523.                 goto FailurePoint20;
  524.             }
  525.  
  526.         switch (SampleObjectGetNumChannels(SampleObject))
  527.             {
  528.                 case eSampleMono:
  529.                     SetIconButtonState(Window->MonoButton,True);
  530.                     break;
  531.                 case eSampleStereo:
  532.                     SetIconButtonState(Window->StereoButton,True);
  533.                     break;
  534.                 default:
  535.                     EXECUTE(PRERR(ForceAbort,"NewSampleWindow:  bad num channels value"));
  536.                     break;
  537.             }
  538.  
  539.         Window->ZoomInButton = NewIconButtonPreparedBitmaps(Window->ScreenID,ZOOMINX,
  540.             ZOOMINY(WinHeight),32,32,ZoomInMouseUp,ZoomInMouseDown,
  541.             ZoomInMouseUp,ZoomInMouseDown,eIconSimpleMode);
  542.         if (Window->ZoomInButton == NIL)
  543.             {
  544.              FailurePoint22:
  545.                 DisposeIconButton(Window->StereoButton);
  546.                 goto FailurePoint21;
  547.             }
  548.  
  549.         Window->ZoomOutButton = NewIconButtonPreparedBitmaps(Window->ScreenID,ZOOMOUTX,
  550.             ZOOMOUTY(WinHeight),32,32,ZoomOutMouseUp,ZoomOutMouseDown,
  551.             ZoomOutMouseUp,ZoomOutMouseDown,eIconSimpleMode);
  552.         if (Window->ZoomOutButton == NIL)
  553.             {
  554.              FailurePoint23:
  555.                 DisposeIconButton(Window->ZoomInButton);
  556.                 goto FailurePoint22;
  557.             }
  558.  
  559.         Window->PlayButton = NewIconButtonPreparedBitmaps(Window->ScreenID,PLAYX,
  560.             PLAYY(WinHeight),32,32,PlayMouseUp,PlayMouseDown,
  561.             PlayMouseUp,PlayMouseDown,eIconSimpleMode);
  562.         if (Window->PlayButton == NIL)
  563.             {
  564.              FailurePoint24:
  565.                 DisposeIconButton(Window->ZoomOutButton);
  566.                 goto FailurePoint23;
  567.             }
  568.  
  569.         /* notify window management */
  570.         Window->MyGenericWindow = CheckInNewWindow(Window->ScreenID,Window,
  571.             (void (*)(void*,MyBoolean,OrdType,OrdType,ModifierFlags))&SampleWindowDoIdle,
  572.             (void (*)(void*))&SampleWindowBecomeActive,
  573.             (void (*)(void*))&SampleWindowBecomeInactive,
  574.             (void (*)(void*))&SampleWindowJustResized,
  575.             (void (*)(OrdType,OrdType,ModifierFlags,void*))&SampleWindowDoMouseDown,
  576.             (void (*)(unsigned char,ModifierFlags,void*))&SampleWindowDoKeyDown,
  577.             (void (*)(void*))&SampleWindowClose,
  578.             (void (*)(void*))&SampleWindowMenuSetup,
  579.             (void (*)(void*,MenuItemType*))&SampleWindowDoMenuCommand);
  580.         if (Window->MyGenericWindow == NIL)
  581.             {
  582.              FailurePoint25:
  583.                 DisposeIconButton(Window->PlayButton);
  584.                 goto FailurePoint24;
  585.             }
  586.  
  587.         Window->MyMenuItem = MakeNewMenuItem(mmWindowMenu,"x",0);
  588.         if (Window->MyMenuItem == NIL)
  589.             {
  590.              FailurePoint26:
  591.                 CheckOutDyingWindow(Window->MyGenericWindow);
  592.                 goto FailurePoint25;
  593.             }
  594.         if (!RegisterWindowMenuItem(Window->MyMenuItem,(void (*)(void*))&ActivateThisWindow,
  595.             Window->ScreenID))
  596.             {
  597.              FailurePoint27:
  598.                 KillMenuItem(Window->MyMenuItem);
  599.                 goto FailurePoint26;
  600.             }
  601.  
  602.         Window->ActiveTextEdit = NIL;
  603.  
  604.         /* install new data into all the edits */
  605.         SampleWindowPutSelectStart(Window);
  606.         SampleWindowPutSelectEnd(Window);
  607.         SampleWindowPutScalingFactor(Window);
  608.         SampleWindowPutOrigin(Window,SampleObjectGetOrigin(SampleObject));
  609.         SampleWindowPutLoopStart(Window,SampleObjectGetLoopStart1(SampleObject));
  610.         SampleWindowPutLoopEnd(Window,SampleObjectGetLoopEnd1(SampleObject));
  611.         SampleWindowPutSamplingRate(Window,SampleObjectGetSamplingRate(SampleObject));
  612.         SampleWindowPutNaturalFrequency(Window,SampleObjectGetNaturalFrequency(SampleObject));
  613.         TextEditHasBeenSaved(Window->SelectionStartEdit);
  614.         TextEditHasBeenSaved(Window->SelectionEndEdit);
  615.         TextEditHasBeenSaved(Window->ScalingEdit);
  616.         TextEditHasBeenSaved(Window->OriginEdit);
  617.         TextEditHasBeenSaved(Window->LoopStartEdit);
  618.         TextEditHasBeenSaved(Window->LoopEndEdit);
  619.         TextEditHasBeenSaved(Window->SamplingRateEdit);
  620.         TextEditHasBeenSaved(Window->NaturalFrequencyEdit);
  621.         UpdateSampleWindowScrollbar(Window);
  622.  
  623.         SampleWindowResetTitlebar(Window);
  624.  
  625.         return Window;
  626.     }
  627.  
  628.  
  629. /* dispose of the window and write all modified data back */
  630. void                                DisposeSampleWindow(SampleWindowRec* Window)
  631.     {
  632.         CheckPtrExistence(Window);
  633.  
  634.         /* save the data */
  635.         if (!SampleWindowWritebackModifiedData(Window))
  636.             {
  637.                 /* failed -- now what? */
  638.             }
  639.  
  640.         /* now that we've recovered all the data, we can delete the things */
  641.         SampleObjectClosingWindowNotify(Window->SampleObject,
  642.             GetWindowXStart(Window->ScreenID),GetWindowYStart(Window->ScreenID),
  643.             GetWindowWidth(Window->ScreenID),GetWindowHeight(Window->ScreenID));
  644.         DeregisterWindowMenuItem(Window->MyMenuItem);
  645.         KillMenuItem(Window->MyMenuItem);
  646.         CheckOutDyingWindow(Window->MyGenericWindow);
  647.         DisposeSampleView(Window->SampleView);
  648.         DisposeTextEdit(Window->NameEdit);
  649.         DisposeTextEdit(Window->SamplingRateEdit);
  650.         DisposeTextEdit(Window->OriginEdit);
  651.         DisposeTextEdit(Window->NaturalFrequencyEdit);
  652.         DisposeTextEdit(Window->LoopStartEdit);
  653.         DisposeTextEdit(Window->LoopEndEdit);
  654.         DisposeTextEdit(Window->SelectionStartEdit);
  655.         DisposeTextEdit(Window->SelectionEndEdit);
  656.         DisposeTextEdit(Window->ScalingEdit);
  657.         DisposeTextEdit(Window->MousePositionEdit);
  658.         DisposeTextEdit(Window->SampleExpressionEdit);
  659.         DisposeScrollBar(Window->Scrollbar);
  660.         DisposeIconButton(Window->Bits8Button);
  661.         DisposeIconButton(Window->Bits16Button);
  662.         DisposeIconButton(Window->MonoButton);
  663.         DisposeIconButton(Window->StereoButton);
  664.         DisposeIconButton(Window->ZoomInButton);
  665.         DisposeIconButton(Window->ZoomOutButton);
  666.         DisposeIconButton(Window->PlayButton);
  667.         KillWindow(Window->ScreenID);
  668.         ReleasePtr((char*)Window);
  669.     }
  670.  
  671.  
  672. /* returns True if any data has been changed since the last time the file was saved */
  673. MyBoolean                        HasSampleWindowBeenModified(SampleWindowRec* Window)
  674.     {
  675.         CheckPtrExistence(Window);
  676.         return DoesSampleViewNeedToBeSaved(Window->SampleView)
  677.             || TextEditDoesItNeedToBeSaved(Window->NameEdit)
  678.             || TextEditDoesItNeedToBeSaved(Window->SamplingRateEdit)
  679.             || TextEditDoesItNeedToBeSaved(Window->OriginEdit)
  680.             || hiftPoints(Window,SelectStart,NetInsertedFrames);
  681.                         AdjustSampleWindowThangs(Window);
  682.                         SampleWindowRedrawThangs(Window);
  683.                         SampleWindowPutSelectStart(Window);
  684.                         SampleWindowPutSelectEnd(Window);
  685.                     }
  686.             }
  687.         else if (MenuItem == mCut)
  688.             {
  689.                 if (Window->ActiveTextEdit != NIL)
  690.                     {
  691.                         TextEditDoMenuCut(Window->ActiveTextEdit);
  692.                     }
  693.                  else
  694.                     {
  695.                         SampleStorageDisplayRec*        DeletedData;
  696.                         long                                                SelectStart;
  697.                         long                                                SelectEnd;
  698.                         long                                                NetInsertedFrames;
  699.  
  700.                         /* sample cut */
  701.                         if (!SampleWindowUndoSetupReturnTrueIfGoAhead(Window))
  702.                             {
  703.                                 /* set up undo; ask user whether to continue if not saved ya? */
  704.                                 return;
  705.                             }
  706.                         SelectStart = GetSampleViewSelectStart(Window->SampleView);
  707.                         SelectEnd = GetSampleViewSelectEnd(Window->SampleView);
  708.                         NetInsertedFrames = - (SelectEnd - SelectStart);
  709.                         DeletedData = ExtractSampleViewSection(Window->SampleView,SelectStart,
  710.                             SelectEnd - SelectStart);
  711.                         if (DeletedData == NIL)
  712.                             {
  713.                              CutSampleFailurePoint:
  714.                                 AlertHalt("There is not enough memory available to "
  715.                                     "complete the cut operation.",NIL);
  716.                                 return;
  717.                             }
  718.                         PutSampleStorageDisplayOnClipboard(DeletedData); /* put it on the clipboard */
  719.                         DisposeSampleStorageDisplay(DeletedData);
  720.                         if (!DeleteSampleViewArea(Window->SampleView,SelectStart,
  721.                             SelectEnd - SelectStart))
  722.                             {
  723.                                 goto CutSampleFailurePoint;
  724.                             }
  725.                         UpdateSampleWindowScrollbar(Window);
  726.                         SampleWindowRedrawUnderThangs(Window);
  727.                         SampleWindowShiftPoints(Window,SelectStart,NetInsertedFrames);
  728.                         AdjustSampleWindowThangs(Window);
  729.                         SampleWindowRedrawThangs(Window);
  730.                         SampleWindowPutSelectStart(Window);
  731.                         SampleWindowPutSelectEnd(Window);
  732.                     }
  733.             }
  734.         else if (MenuItem == mCopy)
  735.             {
  736.                 if (Window->ActiveTextEdit != NIL)
  737.                     {
  738.                         TextEditDoMenuCopy(Window->ActiveTextEdit);
  739.                     }
  740.                  else
  741.                     {
  742.                         SampleStorageDisplayRec*        CopiedData;
  743.                         long                                XSize = GetWindowWidth(Window->ScreenID);
  744.         YSize = GetWindowHeight(Window->ScreenID);
  745.         SetClipRect(Window->ScreenID,XSize - 15,YSize - 15,XSize,YSize);
  746.         DrawBitmap(Window->ScreenID,XSize-15,YSize-15,GetGrowIcon(True/*enablegrowicon*/));
  747.     }
  748.  
  749.  
  750. void                                SampleWindowBecomeInactive(SampleWindowRec* Window)
  751.     {
  752.         OrdType                        XSize;
  753.         OrdType                        YSize;
  754.  
  755.         CheckPtrExistence(Window);
  756.         if (Window->ActiveTextEdit != NIL)
  757.             {
  758.                 DisableTextEditSelection(Window->ActiveTextEdit);
  759.             }
  760.         DisableScrollBar(Window->Scrollbar);
  761.         XSize = GetWindowWidth(Window->ScreenID);
  762.         YSize = GetWindowHeight(Window->ScreenID);
  763.         SetClipRect(Window->ScreenID,XSize - 15,YSize - 15,XSize,YSize);
  764.         DrawBitmap(Window->ScreenID,XSize-15,YSize-15,GetGrowIcon(False/*disablegrowicon*/));
  765.     }
  766.  
  767.  
  768. void                                SampleWindowJustResized(SampleWindowRec* Window)
  769.     {
  770.         OrdType                        XSize;
  771.         OrdType                        YSize;
  772.  
  773.         CheckPtrExistence(Window);
  774.         XSize = GetWindowWidth(Window->ScreenID);
  775.         YSize = GetWindowHeight(Window->ScreenID);
  776.         SetClipRect(Window->ScreenID,0,0,XSize,YSize);
  777.         DrawBoxErase(Window->ScreenID,0,0,XSize,YSize);
  778.  
  779.         SetSampleViewLocation(Window->SampleView,SAMPLEEDITX,
  780.             SAMPLEEDITY + (3 * OVERLINEHEIGHT),
  781.             SAMPLEEDITWIDTH(XSize),SAMPLEEDITHEIGHT(YSize) - 15 - (3 * OVERLINEHEIGHT));
  782.         SetTextEditPosition(Window->SelectionStartEdit,SELECTSTARTEDITX,
  783.             SELECTSTARTEDITY(YSize),SELECTSTARTEDITWIDTH,SELECTSTARTEDITHEIGHT);
  784.         SetTextEditPosition(Window->SelectionEndEdit,SELECTENDEDITX,
  785.             SELECTENDEDITY(YSize),SELECTENDEDITWIDTH,SELECTENDEDITHEIGHT);
  786.         SetTextEditPosition(Window->ScalingEdit,SCALINGEDITX,SCALINGEDITY(YSize),
  787.             SCALINGEDITWIDTH,SCALINGEDITHEIGHT);
  788.         SetTextEditPosition(Window->MousePositionEdit,MOUSEPOSEDITX,
  789.             MOUSEPOSEDITY(YSize),MOUSEPOSEDITWIDTH,MOUSEPOSEDITHEIGHT);
  790.         SetTextEditPosition(Window->SampleExpressionEdit,EXPRESSIONEDITX,
  791.             EXPRESSIONEDITY(YSize),EXPRESSIONEDITWIDTH(XSize),EXPRESSIONEDITHEIGHT);
  792.         SetScrollLocation(Window->Scrollbar,SAMPLEEDITX,SAMPLEEDITY
  793.             + SAMPLEEDITHEIGHT(YSize) - 16,SAMPLEEDITWIDTH(XSize));
  794.         SetIconButtonLocation(Window->Bits8Button,BITS8X,BITS8Y(YSize));
  795.         SetIconButtonLocation(Window->Bits16Button,BITS16X,BITS16Y(YSize));
  796.         SetIconButtonLocation(Window->MonoButton,MONOX,MONOY(YSize));
  797.         SetIconButtonLocation(Window->StereoButton,STEREOX,STEREOY(YSize));
  798.         SetIconButtonLocation(Window->ZoomInButton,ZOOMINX,ZOOMINY(YSize));
  799.         SetIconButtonLocation(Window->ZoomOutButton,ZOOMOUTX,ZOOMOUTY(YSize));
  800.         SetIconButtonLocation(Window->PlayButton,PLAYX,PLAYY(YSize));
  801.     }
  802.  
  803.  
  804. /* helper function to set up the undo & present the user with a dialog */
  805. /* asking if he wants to continue if undo information couldn't be saved. */
  806. /* it returns True if the operation should continue. */
  807. static MyBoolean        SampleWindowUndoSetupReturnTrueIfGoAhead(SampleWindowRec* Window)
  808.     {
  809.         CheckPtrExistence(Window);
  810.         SampleWindowFinalizeCurrentEdit(Window);
  811.         Window->UndoOrigin = SampleObjectGetOrigin(Window->SampleObject);
  812.         Window->UndoLoopStart1 = SampleObjectGetLoopStart1(Window->SampleObject);
  813.         Window->UndoLoopStart2 = SampleObjectGetLoopStart2(Window->SampleObject);
  814.         Window->UndoLoopStart3 = SampleObjectGetLoopStart3(Window->SampleObject);
  815.         Window->UndoLoopEnd1 = SampleObjectGetLoopEnd1(Window->SampleObject);
  816.         Window->UndoLoopEnd2 = SampleObjectGetLoopEnd2(Window->SampleObject);
  817.         Window->UndoLoopEnd3 = SampleObjectGetLoopEnd3(Window->SampleObject);
  818.         if (!SampleViewSetupForUndo(Window->SampleView))
  819.             {
  820.                 switch (AskYesNoCancel("There is not enough memory available to save undo "
  821.                     "information.  Do you want to continue anyway?",NIL,"Continue","Cancel",NIL))
  822.                     {
  823.                         default:
  824.                             EXECUTE(PRERR(ForceAbort,"SampleWindowUndoSetupReturnTrueIfGoAhead:  "
  825.                                 "bad value from AskYesNoCancel"));
  826.                             break;
  827.                         case eYes:
  828.                             return True;
  829.                         case eNo:
  830.                             return False;
  831.                     }
  832.             }
  833.         return True;
  834.     }
  835.  
  836.  
  837. void                                SampleWindowDoMouseDown(OrdType XLoc, OrdType YLoc,
  838.                                             ModifierFlags Modifiers, SampleWindowRec* Window)
  839.     {
  840.         CheckPtrExistence(Window);
  841.         if ((XLoc >= GetWindowWidth(Window->ScreenID) - 15)
  842.             && (XLoc < GetWindowWidth(Window->ScreenID))
  843.             && (YLoc >= GetWindowHeight(Window->ScreenID) - 15)
  844.             && (YLoc < GetWindowHeight(Window->ScreenID)))
  845.             {
  846.                 UserGrowWindow(Window->ScreenID,XLoc,YLoc);
  847.                 SampleWindowJustResized(Window);
  848.             }
  849.         else if ((XLoc >= SAMPLEEDITX) && (YLoc >= SAMPLEEDITY)
  850.             && (XLoc < SAMPLEEDITX + SAMPLEEDITWIDTH(GetWindowWidth(Window->ScreenID)))
  851.             && (YLoc < SAMPLEEDITY + SAMPLEEDITHEIGHT(GetWindowHeight(Window->ScreenID))))
  852.             {
  853.                 SampleWindowFinalizeCurrentEdit(Window);
  854.                 SampleWindowDoMouseDownInEdit(Window,XLoc,YLoc);
  855.             }
  856.         else if (TextEditHitTest(Window->SampleExpressionEdit,XLoc,YLoc))
  857.             {
  858.                 if (Window->ActiveTextEdit != Window->SampleExpressionEdit)
  859.                     {
  860.                         SampleWindowFinalizeCurrentEdit(Window);
  861.                         Window->ActiveTextEdit = Window->SampleExpressionEdit;
  862.                         EnableTextEditSelection(Window->ActiveTextEdit);
  863.                     }
  864.                 TextEditDoMouseDown(Window->SampleExpressionEdit,XLoc,YLoc,Modifiers);
  865.             }
  866.         else if (TextEditHitTest(Window->NameEdit,XLoc,YLoc))
  867.             {
  868.                 if (Window->ActiveTextEdit != Window->NameEdit)
  869.                     {
  870.                         SampleWindowFinalizeCurrentEdit(Window);
  871.                         Window->ActiveTextEdit = Window->NameEdit;
  872.                         EnableTextEditSelection(Window->ActiveTextEdit);
  873.                     }
  874.                 TextEditDoMouseDown(Window->NameEdit,XLoc,YLoc,Modifiers);
  875.             }
  876.         else if (TextEditHitTest(Window->SamplingRateEdit,XLoc,YLoc))
  877.             {
  878.                 if (Window->ActiveTextEdit != Window->SamplingRateEdit)
  879.                     {
  880.                         SampleWindowFinalizeCurrentEdit(Window);
  881.                         Window->ActiveTextEdit = Window->SamplingRateEdit;
  882.                         EnableTextEditSelection(Window->ActiveTextEdit);
  883.                     }
  884.                 TextEditDoMouseDown(Window->SamplingRateEdit,XLoc,YLoc,Modifiers);
  885.             }
  886.         else if (TextEditHitTest(Window->OriginEdit,XLoc,YLoc))
  887.             {
  888.                 if (Window->ActiveTextEdit != Window->OriginEdit)
  889.                     {
  890.                         SampleWindowFinalizeCurrentEdit(Window);
  891.                         Window->ActiveTextEdit = Window->OriginEdit;
  892.                         EnableTextEditSelection(Window->ActiveTextEdit);
  893.                     }
  894.                 TextEditDoMouseDown(Window->OriginEdit,XLoc,YLoc,Modifiers);
  895.             }
  896.         else if (TextEditHitTest(Window->NaturalFrequencyEdit,XLoc,YLoc))
  897.             {
  898.                 if (Window->ActiveTextEdit != Window->NaturalFrequencyEdit)
  899.                     {
  900.                         SampleWindowFinalizeCurrentEdit(Window);
  901.                         Window->ActiveTextEdit = Window->NaturalFrequencyEdit;
  902.                         EnableTextEditSelection(Window->ActiveTextEdit);
  903.                     }
  904.                 TextEditDoMouseDown(Window->NaturalFrequencyEdit,XLoc,YLoc,Modifiers);
  905.             }
  906.         else if (TextEditHitTest(Window->LoopStartEdit,XLoc,YLoc))
  907.             {
  908.                 if (Window->ActiveTextEdit != Window->LoopStartEdit)
  909.                     {
  910.                         SampleWindowFinalizeCurrentEdit(Window);
  911.                         Window->ActiveTextEdit = Window->LoopStartEdit;
  912.                         EnableTextEditSelection(Window->ActiveTextEdit);
  913.                     }
  914.                 TextEditDoMouseDown(Window->LoopStartEdit,XLoc,YLoc,Modifiers);
  915.             }
  916.         else if (TextEditHitTest(Window->LoopEndEdit,XLoc,YLoc))
  917.             {
  918.                 if (Window->ActiveTextEdit != Window->LoopEndEdit)
  919.                     {
  920.                         SampleWindowFinalizeCurrentEdit(Window);
  921.                         Window->ActiveTextEdit = Window->LoopEndEdit;
  922.                         EnableTextEditSelection(Window->ActiveTextEdit);
  923.                     }
  924.                 TextEditDoMouseDown(Window->LoopEndEdit,XLoc,YLoc,Modifiers);
  925.             }
  926.         else if (TextEditHitTest(Window->SelectionStartEdit,XLoc,YLoc))
  927.             {
  928.                 if (Window->ActiveTextEdit != Window->SelectionStartEdit)
  929.                     {
  930.                         SampleWindowFinalizeCurrentEdit(Window);
  931.                         Window->ActiveTextEdit = Window->SelectionStartEdit;
  932.                         EnableTextEditSelection(Window->ActiveTextEdit);
  933.                     }
  934.                 TextEditDoMouseDown(Window->SelectionStartEdit,XLoc,YLoc,Modifiers);
  935.             }
  936.         else if (TextEditHitTest(Window->SelectionEndEdit,XLoc,YLoc))
  937.             {
  938.                 if (Window->ActiveTextEdit != Window->SelectionEndEdit)
  939.                     {
  940.                         SampleWindowFinalizeCurrentEdit(Window);
  941.                         Window->ActiveTextEdit = Window->SelectionEndEdit;
  942.                         EnableTextEditSelection(Window->ActiveTextEdit);
  943.                     }
  944.                 TextEditDoMouseDown(Window->SelectionEndEdit,XLoc,YLoc,Modifiers);
  945.             }
  946.         else if (TextEditHitTest(Window->ScalingEdit,XLoc,YLoc))
  947.             {
  948.                 if (Window->ActiveTextEdit != Window->ScalingEdit)
  949.                     {
  950.                         SampleWindowFinalizeCurrentEdit(Window);
  951.                         Window->ActiveTextEdit = Window->ScalingEdit;
  952.                         EnableTextEditSelection(Window->ActiveTextEdit);
  953.                     }
  954.                 TextEditDoMouseDown(Window->ScalingEdit,XLoc,YLoc,Modifiers);
  955.             }
  956.         else if (IconButtonHitTest(Window->Bits8Button,XLoc,YLoc))
  957.             {
  958.                 if (IconButtonMouseDown(Window->Bits8Button,XLoc,YLoc,NIL,NIL))
  959.                     {
  960.                         if (SampleWindowUndoSetupReturnTrueIfGoAhead(Window))
  961.                             {
  962.                                 if (!SetSampleViewNumBits(Window->SampleView,eSample8bit))
  963.                                     {
  964.                                         AlertHalt("There is not enough memory available to convert the "
  965.                                             "sample to 8-bit format.",NIL);
  966.                                     }
  967.                                  else
  968.                                     {
  969.                                         SetIconButtonState(Window->Bits16Button,False);
  970.                                     }
  971.                                 SampleWindowRedrawThangs(Window);
  972.                             }
  973.                     }
  974.             }
  975.         else if (IconButtonHitTest(Window->Bits16Button,XLoc,YLoc))
  976.             {
  977.                 if (IconButtonMouseDown(Window->Bits16Button,XLoc,YLoc,NIL,NIL))
  978.                     {
  979.                         if (SampleWindowUndoSetupReturnTrueIfGoAhead(Window))
  980.                             {
  981.                                 if (!SetSampleViewNumBits(Window->SampleView,eSample16bit))
  982.                                     {
  983.                                         AlertHalt("There is not enough memory available to convert the "
  984.                                             "sample to 16-bit format.",NIL);
  985.                                     }
  986.                                  else
  987.                                     {
  988.                                         SetIconButtonState(Window->Bits8Button,False);
  989.                                     }
  990.                                 SampleWindowRedrawThangs(Window);
  991.                             }
  992.                     }
  993.             }
  994.         else if (IconButtonHitTest(Window->MonoButton,XLoc,YLoc))
  995.             {
  996.                 if (IconButtonMouseDown(Window->MonoButton,XLoc,YLoc,NIL,NIL))
  997.                     {
  998.                         if (SampleWindowUndoSetupReturnTrueIfGoAhead(Window))
  999.                             {
  1000.                                 if (!SetSampleViewNumChannels(Window->SampleView,eSampleMono))
  1001.                                     {
  1002.                                         AlertHalt("There is not enough memory available to convert the "
  1003.                                             "sample to mono format.",NIL);
  1004.                                     }
  1005.                                  else
  1006.                                     {
  1007.                                         SetIconButtonState(Window->StereoButton,False);
  1008.                                     }
  1009.                                 SampleWindowRedrawThangs(Window);
  1010.                             }
  1011.                     }
  1012.             }
  1013.         else if (IconButtonHitTest(Window->StereoButton,XLoc,YLoc))
  1014.             {
  1015.                 if (IconButtonMouseDown(Window->StereoButton,XLoc,YLoc,NIL,NIL))
  1016.                     {
  1017.                         if (SampleWindowUndoSetupReturnTrueIfGoAhead(Window))
  1018.                             {
  1019.                                 if (!SetSampleViewNumChannels(Window->SampleView,eSampleStereo))
  1020.                                     {
  1021.                                         AlertHalt("There is not enough memory available to convert the "
  1022.                                             "sample to stereo format.",NIL);
  1023.                                     }
  1024.                                  else
  1025.                                     {
  1026.                                         SetIconButtonState(Window->MonoButton,False);
  1027.                                     }
  1028.                                 SampleWindowRedrawThangs(Window);
  1029.                             }
  1030.                     }
  1031.             }
  1032.         else if (IconButtonHitTest(Window->ZoomInButton,XLoc,YLoc))
  1033.             {
  1034.                 if (IconButtonMouseDown(Window->ZoomInButton,XLoc,YLoc,NIL,NIL))
  1035.                     {
  1036.                         double                    NewValue;
  1037.  
  1038.                         NewValue = GetSampleViewHorizontalScale(Window->SampleView) / ZOOMFACTOR;
  1039.                         if (NewValue < MINIMUMHORIZSCALE)
  1040.                             {
  1041.                                 NewValue = MINIMUMHORIZSCALE;
  1042.                             }
  1043.                         SetSampleViewHorizontalScale(Window->SampleView,NewValue);
  1044.                         SampleWindowRedrawThangs(Window);
  1045.                         SampleWindowPutScalingFactor(Window);
  1046.                         UpdateSampleWindowScrollbar(Window);
  1047.                     }
  1048.             }
  1049.         else if (IconButtonHitTest(Window->ZoomOutButton,XLoc,YLoc))
  1050.             {
  1051.                 if (IconButtonMouseDown(Window->ZoomOutButton,XLoc,YLoc,NIL,NIL))
  1052.                     {
  1053.                         double                    NewValue;
  1054.  
  1055.                         NewValue = GetSampleViewHorizontalScale(Window->SampleView) * ZOOMFACTOR;
  1056.                         SetSampleViewHorizontalScale(Window->SampleView,NewValue);
  1057.                         SampleWindowRedrawThangs(Window);
  1058.                         SampleWindowPutScalingFactor(Window);
  1059.                         UpdateSampleWindowScrollbar(Window);
  1060.                     }
  1061.             }
  1062.         else if (IconButtonHitTest(Window->PlayButton,XLoc,YLoc))
  1063.             {
  1064.                 if (IconButtonMouseDown(Window->PlayButton,XLoc,YLoc,
  1065.                     (void (*)(void*, MyBoolean))&SampleTestPlayCallback,Window))
  1066.                     {
  1067.                         if (SampleTestIsItLooping())
  1068.                             {
  1069.                                 SampleTestBreakLoop();
  1070.                             }
  1071.                         while (!SampleTestIsWaitingForCleanup())
  1072.                             {
  1073.                                 if (SampleTestDidErrorOccur() != eSampleTestNoError)
  1074.                                     {
  1075.                                         AlertHalt("An error occurred and the sample couldn't be played.",NIL);
  1076.                                         goto DonePlayingPoint;
  1077.                                     }
  1078.                                 SampleTestPlayCallback(Window,False);
  1079.                             }
  1080.                      DonePlayingPoint:
  1081.                         SampleTestPlayCleanup();
  1082.                         ERROR(SampleTestPlayInProgress(),PRERR(ForceAbort,
  1083.                             "SampleWindowDoMouseDown:  play test failure"));
  1084.                     }
  1085.             }
  1086.         else
  1087.             {
  1088.                 /* finalize edit for click outside any known area */
  1089.                 SampleWindowFinalizeCurrentEdit(Window);
  1090.             }
  1091.     }
  1092.  
  1093.  
  1094. void                                SampleWindowDoKeyDown(unsigned char KeyCode,
  1095.                                             ModifierFlags Modifiers,SampleWindowRec* Window)
  1096.     {
  1097.         CheckPtrExistence(Window);
  1098.         if ((KeyCode == 13) && (Window->ActiveTextEdit != Window->SampleExpressionEdit))
  1099.             {
  1100.                 SampleWindowFinalizeCurrentEdit(Window);
  1101.             }
  1102.         else if ((KeyCode == 9) && (Window->ActiveTextEdit != Window->SampleExpressionEdit))
  1103.             {
  1104.                 TextEditRec*            WasTheActiveTextEdit;
  1105.  
  1106.                 WasTheActiveTextEdit = Window->ActiveTextEdit;
  1107.                 SampleWindowFinalizeCurrentEdit(Window);
  1108.                 if ((Modifiers & eShiftKey) != 0)
  1109.                     {
  1110.                         if (WasTheActiveTextEdit == Window->ScalingEdit)
  1111.                             {
  1112.                                 Window->ActiveTextEdit = Window->SelectionEndEdit;
  1113.                             }
  1114.                         else if (WasTheActiveTextEdit == Window->SelectionEndEdit)
  1115.                             {
  1116.                                 Window->ActiveTextEdit = Window->SelectionStartEdit;
  1117.                             }
  1118.                         else if (WasTheActiveTextEdit == Window->SelectionEndEdit)
  1119.                             {
  1120.                                 Window->ActiveTextEdit = Window->SelectionStartEdit;
  1121.                             }
  1122.                         else if (WasTheActiveTextEdit == Window->SelectionStartEdit)
  1123.                             {
  1124.                                 Window->ActiveTextEdit = Window->LoopEndEdit;
  1125.                             }
  1126.                         else if (WasTheActiveTextEdit == Window->LoopEndEdit)
  1127.                             {
  1128.                                 Window->ActiveTextEdit = Window->LoopStartEdit;
  1129.                             }
  1130.                         else if (WasTheActiveTextEdit == Window->LoopStartEdit)
  1131.                             {
  1132.                                 Window->ActiveTextEdit = Window->NaturalFrequencyEdit;
  1133.                             }
  1134.                         else if (WasTheActiveTextEdit == Window->NaturalFrequencyEdit)
  1135.                             {
  1136.                                 Window->ActiveTextEdit = Window->OriginEdit;
  1137.                             }
  1138.                         else if (WasTheActiveTextEdit == Window->OriginEdit)
  1139.                             {
  1140.                                 Window->ActiveTextEdit = Window->SamplingRateEdit;
  1141.                             }
  1142.                         else if (WasTheActiveTextEdit == Window->SamplingRateEdit)
  1143.                             {
  1144.                                 Window->ActiveTextEdit = Window->NameEdit;
  1145.                             }
  1146.                         else
  1147.                             {
  1148.                                 Window->ActiveTextEdit = Window->ScalingEdit;
  1149.                             }
  1150.                     }
  1151.                  else
  1152.                     {
  1153.                         if (WasTheActiveTextEdit == Window->NameEdit)
  1154.                             {
  1155.                                 Window->ActiveTextEdit = Window->SamplingRateEdit;
  1156.                             }
  1157.                         else if (WasTheActiveTextEdit == Window->SamplingRateEdit)
  1158.                             {
  1159.                                 Window->ActiveTextEdit = Window->OriginEdit;
  1160.                             }
  1161.                         else if (WasTheActiveTextEdit == Window->OriginEdit)
  1162.                             {
  1163.                                 Window->ActiveTextEdit = Window->NaturalFrequencyEdit;
  1164.                             }
  1165.                         else if (WasTheActiveTextEdit == Window->NaturalFrequencyEdit)
  1166.                             {
  1167.                                 Window->ActiveTextEdit = Window->LoopStartEdit;
  1168.                             }
  1169.                         else if (WasTheActiveTextEdit == Window->LoopStartEdit)
  1170.                             {
  1171.                                 Window->ActiveTextEdit = Window->LoopEndEdit;
  1172.                             }
  1173.                         else if (WasTheActiveTextEdit == Window->LoopEndEdit)
  1174.                             {
  1175.                                 Window->ActiveTextEdit = Window->SelectionStartEdit;
  1176.                             }
  1177.                         else if (WasTheActiveTextEdit == Window->SelectionStartEdit)
  1178.                             {
  1179.                                 Window->ActiveTextEdit = Window->SelectionEndEdit;
  1180.                             }
  1181.                         else if (WasTheActiveTextEdit == Window->SelectionEndEdit)
  1182.                             {
  1183.                                 Window->ActiveTextEdit = Window->ScalingEdit;
  1184.                             }
  1185.                         else
  1186.                             {
  1187.                                 Window->ActiveTextEdit = Window->NameEdit;
  1188.                             }
  1189.                     }
  1190.                 EnableTextEditSelection(Window->ActiveTextEdit);
  1191.                 TextEditDoMenuSelectAll(Window->ActiveTextEdit);
  1192.             }
  1193.         else if ((KeyCode == 8) && (Window->ActiveTextEdit == NIL))
  1194.             {
  1195.                 /* for delete key with no text box open, delete any selected range */
  1196.                 /* of the sample.  since ActiveTextEdit == NIL, this will be sent */
  1197.                 /* to the appropriate handler.  however, we must make sure there is */
  1198.                 /* a valid selection first, since SampleWindowMenuSetup usually does that. */
  1199.                 if (SampleViewIsThereValidSelection(Window->SampleView))
  1200.                     {
  1201.                         SampleWindowDoMenuCommand(Window,mClear);
  1202.                     }
  1203.             }
  1204.         else
  1205.             {
  1206.                 if (Window->ActiveTextEdit != NIL)
  1207.                     {
  1208.                         TextEditDoKeyPressed(Window->ActiveTextEdit,KeyCode,Modifiers);
  1209.                     }
  1210.             }
  1211.     }
  1212.  
  1213.  
  1214. void                                SampleWindowClose(SampleWindowRec* Window)
  1215.     {
  1216.         CheckPtrExistence(Window);
  1217.         DisposeSampleWindow(Window);
  1218.     }
  1219.  
  1220.  
  1221. void                                SampleWindowUpdator(SampleWindowRec* Window)
  1222.     {
  1223.         OrdType                        Height;
  1224.         OrdType                        Width;
  1225.         FontType                    ScreenFont;
  1226.  
  1227.         CheckPtrExistence(Window);
  1228.  
  1229.         Width = GetWindowWidth(Window->ScreenID);
  1230.         Height = GetWindowHeight(Window->ScreenID);
  1231.         ScreenFont = GetScreenFont();
  1232.  
  1233.         /* redraw edit boxes */
  1234.         RedrawSampleView(Window->SampleView);
  1235.         SampleWindowRedrawThangs(Window);
  1236.         TextEditFullRedraw(Window->NameEdit);
  1237.         TextEditFullRedraw(Window->SamplingRateEdit);
  1238.         TextEditFullRedraw(Window->OriginEdit);
  1239.         TextEditFullRedraw(Window->NaturalFrequencyEdit);
  1240.         TextEditFullRedraw(Window->LoopStartEdit);
  1241.         TextEditFullRedraw(Window->LoopEndEdit);
  1242.         TextEditFullRedraw(Window->SelectionStartEdit);
  1243.         TextEditFullRedraw(Window->SelectionEndEdit);
  1244.         TextEditFullRedraw(Window->ScalingEdit);
  1245.         TextEditFullRedraw(Window->MousePositionEdit);
  1246.         TextEditFullRedraw(Window->SampleExpressionEdit);
  1247.         RedrawScrollBar(Window->Scrollbar);
  1248.         RedrawIconButton(Window->Bits8Button);
  1249.         RedrawIconButton(Window->Bits16Button);
  1250.         RedrawIconButton(Window->MonoButton);
  1251.         RedrawIconButton(Window->StereoButton);
  1252.         RedrawIconButton(Window->ZoomInButton);
  1253.         RedrawIconButton(Window->ZoomOutButton);
  1254.         RedrawIconButton(Window->PlayButton);
  1255.  
  1256.         /* redraw static titles */
  1257.         SetClipRect(Window->ScreenID,0,0,Width,Height);
  1258.         DrawTextLine(Window->ScreenID,ScreenFont,9,"Sample Name:",12,
  1259.             SAMPLENAMEX,SAMPLENAMEY,ePlain);
  1260.         DrawTextLine(Window->ScreenID,ScreenFont,9,"Sampling Rate:",14,
  1261.             SAMPLERATEX,SAMPLERATEY,ePlain);
  1262.         DrawTextLine(Window->ScreenID,ScreenFont,9,"Origin:",7,ORIGINX,ORIGINY,ePlain);
  1263.         DrawTextLine(Window->ScreenID,ScreenFont,9,"Natural Freq:",13,
  1264.             BASEFREQX,BASEFREQY,ePlain);
  1265.         switch (Window->EditingWhichLoop)
  1266.             {
  1267.                 default:
  1268.                     EXECUTE(PRERR(ForceAbort,"SampleWindowUpdator:  bad value in EditingWhichLoop"));
  1269.                     break;
  1270.                 case eSampleLoop1:
  1271.                     DrawTextLine(Window->ScreenID,ScreenFont,9,"Loop Start 1:",13,
  1272.                         LOOPSTARTX,LOOPSTARTY,ePlain);
  1273.                     break;
  1274.                 case eSampleLoop2:
  1275.                     DrawTextLine(Window->ScreenID,ScreenFont,9,"Loop Start 2:",13,
  1276.                         LOOPSTARTX,LOOPSTARTY,ePlain);
  1277.                     break;
  1278.                 case eSampleLoop3:
  1279.                     DrawTextLine(Window->ScreenID,ScreenFont,9,"Loop Start 3:",13,
  1280.                         LOOPSTARTX,LOOPSTARTY,ePlain);
  1281.                     break;
  1282.             }
  1283.         switch (Window->EditingWhichLoop)
  1284.             {
  1285.                 default:
  1286.                     EXECUTE(PRERR(ForceAbort,"SampleWindowUpdator:  bad value in EditingWhichLoop"));
  1287.                     break;
  1288.                 case eSampleLoop1:
  1289.                     DrawTextLine(Window->ScreenID,ScreenFont,9,"Loop End 1:",11,
  1290.                         LOOPENDX,LOOPENDY,ePlain);
  1291.                     break;
  1292.                 case eSampleLoop2:
  1293.                     DrawTextLine(Window->ScreenID,ScreenFont,9,"Loop End 2:",11,
  1294.                         LOOPENDX,LOOPENDY,ePlain);
  1295.                     break;
  1296.                 case eSampleLoop3:
  1297.                     DrawTextLine(Window->ScreenID,ScreenFont,9,"Loop End 3:",11,
  1298.                         LOOPENDX,LOOPENDY,ePlain);
  1299.                     break;
  1300.             }
  1301.         DrawTextLine(Window->ScreenID,ScreenFont,9,"Select Start:",13,
  1302.             SELECTSTARTX,SELECTSTARTY(Height),ePlain);
  1303.         DrawTextLine(Window->ScreenID,ScreenFont,9,"Select End:",11,
  1304.             SELECTENDX,SELECTENDY(Height),ePlain);
  1305.         DrawTextLine(Window->ScreenID,ScreenFont,9,"Mouse Location:",15,
  1306.             MOUSELOCX,MOUSELOCY(Height),ePlain);
  1307.         DrawTextLine(Window->ScreenID,ScreenFont,9,"Scaling Factor:",15,
  1308.             SCALINGX,SCALINGY(Height),ePlain);
  1309.  
  1310.         /* grow icon */
  1311.         SetClipRect(Window->ScreenID,Width - 15,Height - 15,Width,Height);
  1312.         DrawBitmap(Window->ScreenID,Width - 15,Height - 15,
  1313.             GetGrowIcon(Window->MyGenericWindow == GetCurrentWindowID()));
  1314.     }
  1315.  
  1316.  
  1317. void                                SampleWindowMenuSetup(SampleWindowRec* Window)
  1318.     {
  1319.         CheckPtrExistence(Window);
  1320.         MainWindowEnableGlobalMenus(Window->MainWindow);
  1321.  
  1322.         if (Window->ActiveTextEdit != NIL)
  1323.             {
  1324.                 /* editing text, enable menu items for text entry */
  1325.                 EnableMenuItem(mPaste);
  1326.                 ChangeItemName(mPaste,"Paste Text");
  1327.                 if (TextEditIsThereValidSelection(Window->ActiveTextEdit))
  1328.                     {
  1329.                         EnableMenuItem(mCut);
  1330.                         ChangeItemName(mCut,"Cut Text");
  1331.                         EnableMenuItem(mCopy);
  1332.                         ChangeItemName(mCopy,"Copy Text");
  1333.                         EnableMenuItem(mClear);
  1334.                         ChangeItemName(mClear,"Clear Text");
  1335.                     }
  1336.                 EnableMenuItem(mSelectAll);
  1337.                 ChangeItemName(mSelectAll,"Select All Text");
  1338.                 if (TextEditCanWeUndo(Window->ActiveTextEdit))
  1339.                     {
  1340.                         EnableMenuItem(mUndo);
  1341.                         ChangeItemName(mUndo,"Undo Text Change");
  1342.                     }
  1343.                 EnableMenuItem(mShiftLeft);
  1344.                 EnableMenuItem(mShiftRight);
  1345.                 EnableMenuItem(mBalanceParens);
  1346.                 EnableMenuItem(mFind);
  1347.                 if (PtrSize(GlobalSearchString) != 0)
  1348.                     {
  1349.                         EnableMenuItem(mFindAgain);
  1350.                         if (TextEditIsThereValidSelection(Window->ActiveTextEdit))
  1351.                             {
  1352.                                 EnableMenuItem(mReplace);
  1353.                                 EnableMenuItem(mReplaceAndFindAgain);
  1354.                             }
  1355.                     }
  1356.                 if (TextEditIsThereValidSelection(Window->ActiveTextEdit))
  1357.                     {
  1358.                         EnableMenuItem(mEnterSelection);
  1359.                     }
  1360.                 EnableMenuItem(mShowSelection);
  1361.             }
  1362.          else
  1363.             {
  1364.                 /* editing the sample */
  1365.                 EnableMenuItem(mPaste);
  1366.                 ChangeItemName(mPaste,"Paste Sample");
  1367.                 if (SampleViewIsThereValidSelection(Window->SampleView))
  1368.                     {
  1369.                         EnableMenuItem(mCut);
  1370.                         ChangeItemName(mCut,"Cut Sample");
  1371.                         EnableMenuItem(mCopy);
  1372.                         ChangeItemName(mCopy,"Copy Sample");
  1373.                         EnableMenuItem(mClear);
  1374.                         ChangeItemName(mClear,"Clear Sample");
  1375.                     }
  1376.                 EnableMenuItem(mSelectAll);
  1377.                 ChangeItemName(mSelectAll,"Select All Sample");
  1378.                 if (SampleViewUndoAvailable(Window->SampleView))
  1379.                     {
  1380.                         EnableMenuItem(mUndo);
  1381.                         ChangeItemName(mUndo,"Undo Sample Edit");
  1382.                     }
  1383.             }
  1384.  
  1385.         EnableMenuItem(mCloseFile);
  1386.         ChangeItemName(mCloseFile,"Close Sample Editor");
  1387.         EnableMenuItem(mEvaluateCalc);
  1388.         ChangeItemName(mDeleteObject,"Delete Sample");
  1389.         EnableMenuItem(mDeleteObject);
  1390.  
  1391.         EnableMenuItem(mSampleEditLoop1);
  1392.         EnableMenuItem(mSampleEditLoop2);
  1393.         EnableMenuItem(mSampleEditLoop3);
  1394.         switch (Window->EditingWhichLoop)
  1395.             {
  1396.                 default:
  1397.                     EXECUTE(PRERR(ForceAbort,
  1398.                         "SampleWindowMenuSetup:  bad value in EditingWhichLoop"));
  1399.                     break;
  1400.                 case eSampleLoop1:
  1401.                     SetItemCheckmark(mSampleEditLoop1);
  1402.                     break;
  1403.                 case eSampleLoop2:
  1404.                     SetItemCheckmark(mSampleEditLoop2);
  1405.                     break;
  1406.                 case eSampleLoop3:
  1407.                     SetItemCheckmark(mSampleEditLoop3);
  1408.                     break;
  1409.             }
  1410.  
  1411.         EnableMenuItem(mExportWAVFormat);
  1412.         EnableMenuItem(mExportRAWFormat);
  1413.         EnableMenuItem(mExportAIFFFormat);
  1414.  
  1415.         SetItemCheckmark(Window->MyMenuItem);
  1416.     }
  1417.  
  1418.  
  1419. void                                SampleWindowDoMenuCommand(SampleWindowRec* Window,
  1420.                                             MenuItemType* MenuItem)
  1421.     {
  1422.         CheckPtrExistence(Window);
  1423.         if (MainWindowDoGlobalMenuItem(Window->MainWindow,MenuItem))
  1424.             {
  1425.             }
  1426.         else if (MenuItem == mPaste)
  1427.             {
  1428.                 if (Window->ActiveTextEdit != NIL)
  1429.                     {
  1430.                         TextEditDoMenuPaste(Window->ActiveTextEdit);
  1431.                     }
  1432.                  else
  1433.                     {
  1434.                         long                                                SelectStart;
  1435.                         long                                                SelectEnd;
  1436.                         SampleStorageDisplayRec*        ClipboardData;
  1437.                         long                                                NetInsertedFrames;
  1438.  
  1439.                         /* paste sample */
  1440.                         if (!SampleWindowUndoSetupReturnTrueIfGoAhead(Window))
  1441.                             {
  1442.                                 /* set up undo; ask user whether to continue if not saved ya? */
  1443.                                 return;
  1444.                             }
  1445.                         ClipboardData = GetSampleStorageDisplayFromClipboard();
  1446.                         if (ClipboardData == NIL)
  1447.                             {
  1448.                              PasteFailurePoint1:
  1449.                                 return;
  1450.                             }
  1451.                         SelectStart = GetSampleViewSelectStart(Window->SampleView);
  1452.                         SelectEnd = GetSampleViewSelectEnd(Window->SampleView);
  1453.                         NetInsertedFrames = - (SelectEnd - SelectStart);
  1454.                         if (!DeleteSampleViewArea(Window->SampleView,SelectStart,
  1455.                             SelectEnd - SelectStart))
  1456.                             {
  1457.                              PasteFailurePoint2:
  1458.                                 DisposeSampleStorageDisplay(ClipboardData);
  1459.                                 goto PasteFailurePoint1;
  1460.                             }
  1461.                         if (!InsertSampleViewSection(Window->SampleView,SelectStart,ClipboardData))
  1462.                             {
  1463.                              PasteFailurePoint3:
  1464.                                 goto PasteFailurePoint2;
  1465.                             }
  1466.                         NetInsertedFrames += GetSampleStorageDisplayNumFrames(ClipboardData);
  1467.                         DisposeSampleStorageDisplay(ClipboardData);
  1468.                         UpdateSampleWindowScrollbar(Window);
  1469.                         SampleWindowRedrawUnderThangs(Window);
  1470.                         SampleWindowShiftPoints(Window,SelectStart,NetInsertedFrames);
  1471.                         AdjustSampleWindowThangs(Window);
  1472.                         SampleWindowRedrawThangs(Window);
  1473.                         SampleWindowPutSelectStart(Window);
  1474.                         SampleWindowPutSelectEnd(Window);
  1475.                     }
  1476.             }
  1477.         else if (MenuItem == mCut)
  1478.             {
  1479.                 if (Window->ActiveTextEdit != NIL)
  1480.                     {
  1481.                         TextEditDoMenuCut(Window->ActiveTextEdit);
  1482.                     }
  1483.                  else
  1484.                     {
  1485.                         SampleStorageDisplayRec*        DeletedData;
  1486.                         long                                                SelectStart;
  1487.                         long                                                SelectEnd;
  1488.                         long                                                NetInsertedFrames;
  1489.  
  1490.                         /* sample cut */
  1491.                         if (!SampleWindowUndoSetupReturnTrueIfGoAhead(Window))
  1492.                             {
  1493.                                 /* set up undo; ask user whether to continue if not saved ya? */
  1494.                                 return;
  1495.                             }
  1496.                         SelectStart = GetSampleViewSelectStart(Window->SampleView);
  1497.                         SelectEnd = GetSampleViewSelectEnd(Window->SampleView);
  1498.                         NetInsertedFrames = - (SelectEnd - SelectStart);
  1499.                         DeletedData = ExtractSampleViewSection(Window->SampleView,SelectStart,
  1500.                             SelectEnd - SelectStart);
  1501.                         if (DeletedData == NIL)
  1502.                             {
  1503.                              CutSampleFailurePoint:
  1504.                                 AlertHalt("There is not enough memory available to "
  1505.                                     "complete the cut operation.",NIL);
  1506.                                 return;
  1507.                             }
  1508.                         PutSampleStorageDisplayOnClipboard(DeletedData); /* put it on the clipboard */
  1509.                         DisposeSampleStorageDisplay(DeletedData);
  1510.                         if (!DeleteSampleViewArea(Window->SampleView,SelectStart,
  1511.                             SelectEnd - SelectStart))
  1512.                             {
  1513.                                 goto CutSampleFailurePoint;
  1514.                             }
  1515.                         UpdateSampleWindowScrollbar(Window);
  1516.                         SampleWindowRedrawUnderThangs(Window);
  1517.                         SampleWindowShiftPoints(Window,SelectStart,NetInsertedFrames);
  1518.                         AdjustSampleWindowThangs(Window);
  1519.                         SampleWindowRedrawThangs(Window);
  1520.                         SampleWindowPutSelectStart(Window);
  1521.                         SampleWindowPutSelectEnd(Window);
  1522.                     }
  1523.             }
  1524.         else if (MenuItem == mCopy)
  1525.             {
  1526.                 if (Window->ActiveTextEdit != NIL)
  1527.                     {
  1528.                         TextEditDoMenuCopy(Window->ActiveTextEdit);
  1529.                     }
  1530.                  else
  1531.                     {
  1532.                         SampleStorageDisplayRec*        CopiedData;
  1533.                         long                                                SelectStart;
  1534.                         long                                                SelectEnd;
  1535.  
  1536.                         SelectStart = GetSampleViewSelectStart(Window->SampleView);
  1537.                         SelectEnd = GetSampleViewSelectEnd(Window->SampleView);
  1538.                         CopiedData = ExtractSampleViewSection(Window->SampleView,SelectStart,
  1539.                             SelectEnd - SelectStart);
  1540.                         if (CopiedData == NIL)
  1541.                             {
  1542.                                 AlertHalt("There is not enough memory available to "
  1543.                                     "complete the copy operation.",NIL);
  1544.                                 return;
  1545.                             }
  1546.                         PutSampleStorageDisplayOnClipboard(CopiedData); /* put it on the clipboard */
  1547.                         DisposeSampleStorageDisplay(CopiedData);
  1548.                     }
  1549.             }
  1550.         else if (MenuItem == mClear)
  1551.             {
  1552.                 if (Window->ActiveTextEdit != NIL)
  1553.                     {
  1554.                         TextEditDoMenuClear(Window->ActiveTextEdit);
  1555.                     }
  1556.                  else
  1557.                     {
  1558.                         long                                SelectStart;
  1559.                         long                                SelectEnd;
  1560.                         long                                NetInsertedFrames;
  1561.  
  1562.                         /* sample delete */
  1563.                         if (!SampleWindowUndoSetupReturnTrueIfGoAhead(Window))
  1564.                             {
  1565.                                 /* set up undo; ask user whether to continue if not saved ya? */
  1566.                                 return;
  1567.                             }
  1568.                         SelectStart = GetSampleViewSelectStart(Window->SampleView);
  1569.                         SelectEnd = GetSampleViewSelectEnd(Window->SampleView);
  1570.                         NetInsertedFrames = - (SelectEnd - SelectStart);
  1571.                         if (!DeleteSampleViewArea(Window->SampleView,SelectStart,
  1572.                             SelectEnd - SelectStart))
  1573.                             {
  1574.                                 AlertHalt("There is not enough memory available to "
  1575.                                     "complete the clear operation.",NIL);
  1576.                                 return;
  1577.                             }
  1578.                         UpdateSampleWindowScrollbar(Window);
  1579.                         SampleWindowRedrawUnderThangs(Window);
  1580.                         SampleWindowShiftPoints(Window,SelectStart,NetInsertedFrames);
  1581.                         AdjustSampleWindowThangs(Window);
  1582.                         SampleWindowRedrawThangs(Window);
  1583.                         SampleWindowPutSelectStart(Window);
  1584.                         SampleWindowPutSelectEnd(Window);
  1585.                     }
  1586.             }
  1587.         else if (MenuItem == mSelectAll)
  1588.             {
  1589.                 if (Window->ActiveTextEdit != NIL)
  1590.                     {
  1591.                         TextEditDoMenuSelectAll(Window->ActiveTextEdit);
  1592.                     }
  1593.                  else
  1594.                     {
  1595.                         SetSampleViewSelection(Window->SampleView,0,
  1596.                             GetSampleViewNumFrames(Window->SampleView));
  1597.                         SampleWindowPutSelectStart(Window);
  1598.                         SampleWindowPutSelectEnd(Window);
  1599.                     }
  1600.             }
  1601.         else if (MenuItem == mShowSelection)
  1602.             {
  1603.                 if (Window->ActiveTextEdit != NIL)
  1604.                     {
  1605.                         TextEditShowSelection(Window->ActiveTextEdit);
  1606.                     }
  1607.                  else
  1608.                     {
  1609.                     }
  1610.             }
  1611.         else if (MenuItem == mUndo)
  1612.             {
  1613.                 if (Window->ActiveTextEdit != NIL)
  1614.                     {
  1615.                         /* this is the one for text editing */
  1616.                         TextEditDoMenuUndo(Window->ActiveTextEdit);
  1617.                     }
  1618.                  else
  1619.                     {
  1620.                         /* this is the one for samples */
  1621.                         SampleWindowFinalizeCurrentEdit(Window);
  1622.                         if (!SampleViewUndo(Window->SampleView))
  1623.                             {
  1624.                                 AlertHalt("There was not enough memory available to undo the sample "
  1625.                                     "edit.  Try closing some other windows to free up memory.",NIL);
  1626.                             }
  1627.                         switch (GetSampleViewNumChannels(Window->SampleView))
  1628.                             {
  1629.                                 case eSampleMono:
  1630.                                     SetIconButtonState(Window->MonoButton,True);
  1631.                                     SetIconButtonState(Window->StereoButton,False);
  1632.                                     break;
  1633.                                 case eSampleStereo:
  1634.                                     SetIconButtonState(Window->MonoButton,False);
  1635.                                     SetIconButtonState(Window->StereoButton,True);
  1636.                                     break;
  1637.                                 default:
  1638.                                     EXECUTE(PRERR(ForceAbort,
  1639.                                         "SampleWindowDoMenuCommand:  bad num channels value"));
  1640.                                     break;
  1641.                             }
  1642.                         switch (GetSampleViewNumBits(Window->SampleView))
  1643.                             {
  1644.                                 case eSample8bit:
  1645.                                     SetIconButtonState(Window->Bits8Button,True);
  1646.                                     SetIconButtonState(Window->Bits16Button,False);
  1647.                                     break;
  1648.                                 case eSample16bit:
  1649.                                     SetIconButtonState(Window->Bits8Button,False);
  1650.                                     SetIconButtonState(Window->Bits16Button,True);
  1651.                                     break;
  1652.                                 default:
  1653.                                     EXECUTE(PRERR(ForceAbort,
  1654.                                         "SampleWindowDoMenuCommand:  bad number of bits value"));
  1655.                                     break;
  1656.                             }
  1657.                         /* restore some crud */
  1658.                         SampleObjectPutOrigin(Window->SampleObject,Window->UndoOrigin);
  1659.                         SampleObjectPutLoopStart1(Window->SampleObject,Window->UndoLoopStart1);
  1660.                         SampleObjectPutLoopStart2(Window->SampleObject,Window->UndoLoopStart2);
  1661.                         SampleObjectPutLoopStart3(Window->SampleObject,Window->UndoLoopStart3);
  1662.                         SampleObjectPutLoopEnd1(Window->SampleObject,Window->UndoLoopEnd1);
  1663.                         SampleObjectPutLoopEnd2(Window->SampleObject,Window->UndoLoopEnd2);
  1664.                         SampleObjectPutLoopEnd3(Window->SampleObject,Window->UndoLoopEnd3);
  1665.                         /* fix up edit versions */
  1666.                         SampleWindowPutOrigin(Window,Window->UndoOrigin);
  1667.                         switch (Window->EditingWhichLoop)
  1668.                             {
  1669.                                 default:
  1670.                                     EXECUTE(PRERR(ForceAbort,
  1671.                                         "SampleWindowDoMenuCommand:  bad value in EditingWhichLoop"));
  1672.                                     break;
  1673.                                 case eSampleLoop1:
  1674.                                     SampleWindowPutLoopStart(Window,Window->UndoLoopStart1);
  1675.                                     SampleWindowPutLoopEnd(Window,Window->UndoLoopEnd1);
  1676.                                     break;
  1677.                                 case eSampleLoop2:
  1678.                                     SampleWindowPutLoopStart(Window,Window->UndoLoopStart2);
  1679.                                     SampleWindowPutLoopEnd(Window,Window->UndoLoopEnd2);
  1680.                                     break;
  1681.                                 case eSampleLoop3:
  1682.                                     SampleWindowPutLoopStart(Window,Window->UndoLoopStart3);
  1683.                                     SampleWindowPutLoopEnd(Window,Window->UndoLoopEnd3);
  1684.                                     break;
  1685.                             }
  1686.                     }
  1687.             }
  1688.         else if (MenuItem == mCloseFile)
  1689.             {
  1690.                 SampleWindowClose(Window);
  1691.             }
  1692.         else if (MenuItem == mEvaluateCalc)
  1693.             {
  1694.                 if (!SampleWindowUndoSetupReturnTrueIfGoAhead(Window))
  1695.                     {
  1696.                         /* set up undo; ask user whether to continue if not saved ya? */
  1697.                         return;
  1698.                     }
  1699.                 if (Window->ActiveTextEdit != Window->SampleExpressionEdit)
  1700.                     {
  1701.                         SampleWindowFinalizeCurrentEdit(Window);
  1702.                     }
  1703.                 SampleWindowPerformCalculation(Window);
  1704.             }
  1705.         else if (MenuItem == mShiftLeft)
  1706.             {
  1707.                 TextEditShiftSelectionLeftOneTab(Window->ActiveTextEdit);
  1708.             }
  1709.         else if (MenuItem == mShiftRight)
  1710.             {
  1711.                 TextEditShiftSelectionRightOneTab(Window->ActiveTextEdit);
  1712.             }
  1713.         else if (MenuItem == mBalanceParens)
  1714.             {
  1715.                 TextEditBalanceParens(Window->ActiveTextEdit);
  1716.             }
  1717.         else if (MenuItem == mDeleteObject)
  1718.             {
  1719.                 SampleListDeleteSample(Window->SampleList,Window->SampleObject);
  1720.             }
  1721.         else if (MenuItem == mSampleEditLoop1)
  1722.             {
  1723.                 OrdType                        XSize;
  1724.                 OrdType                        YSize;
  1725.  
  1726.                 SampleWindowFinalizeCurrentEdit(Window);
  1727.                 switch (Window->EditingWhichLoop)
  1728.                     {
  1729.                         default:
  1730.                             EXECUTE(PRERR(ForceAbort,
  1731.                                 "SampleWindowDoMenuCommand:  bad value in EditingWhichLoop"));
  1732.                             break;
  1733.                         case eSampleLoop1:
  1734.                             /* switch from 1 to 1 -- no change */
  1735.                             break;
  1736.                         case eSampleLoop2:
  1737.                             /* put away 2 */
  1738.                             SampleObjectPutLoopStart2(Window->SampleObject,
  1739.                                 SampleWindowGetLoopStart(Window));
  1740.                             SampleObjectPutLoopEnd2(Window->SampleObject,
  1741.                                 SampleWindowGetLoopEnd(Window));
  1742.                             /* load 1 */
  1743.                             SampleWindowPutLoopStart(Window,
  1744.                                 SampleObjectGetLoopStart1(Window->SampleObject));
  1745.                             SampleWindowPutLoopEnd(Window,
  1746.                                 SampleObjectGetLoopEnd1(Window->SampleObject));
  1747.                             break;
  1748.                         case eSampleLoop3:
  1749.                             /* put away 3 */
  1750.                             SampleObjectPutLoopStart3(Window->SampleObject,
  1751.                                 SampleWindowGetLoopStart(Window));
  1752.                             SampleObjectPutLoopEnd3(Window->SampleObject,
  1753.                                 SampleWindowGetLoopEnd(Window));
  1754.                             /* load 1 */
  1755.                             SampleWindowPutLoopStart(Window,
  1756.                                 SampleObjectGetLoopStart1(Window->SampleObject));
  1757.                             SampleWindowPutLoopEnd(Window,
  1758.                                 SampleObjectGetLoopEnd1(Window->SampleObject));
  1759.                             break;
  1760.                     }
  1761.                 Window->EditingWhichLoop = eSampleLoop1;
  1762.                 XSize = GetWindowWidth(Window->ScreenID);
  1763.                 YSize = GetWindowHeight(Window->ScreenID);
  1764.                 SetClipRect(Window->ScreenID,0,0,XSize,YSize);
  1765.                 DrawBoxErase(Window->ScreenID,0,0,XSize,YSize);
  1766.                 SampleWindowUpdator(Window);
  1767.             }
  1768.         else if (MenuItem == mSampleEditLoop2)
  1769.             {
  1770.                 OrdType                        XSize;
  1771.                 OrdType                        YSize;
  1772.  
  1773.                 SampleWindowFinalizeCurrentEdit(Window);
  1774.                 switch (Window->EditingWhichLoop)
  1775.                     {
  1776.                         default:
  1777.                             EXECUTE(PRERR(ForceAbort,
  1778.                                 "SampleWindowDoMenuCommand:  bad value in EditingWhichLoop"));
  1779.                             break;
  1780.                         case eSampleLoop1:
  1781.                             /* put away 1 */
  1782.                             SampleObjectPutLoopStart1(Window->SampleObject,
  1783.                                 SampleWindowGetLoopStart(Window));
  1784.                             SampleObjectPutLoopEnd1(Window->SampleObject,
  1785.                                 SampleWindowGetLoopEnd(Window));
  1786.                             /* load 2 */
  1787.                             SampleWindowPutLoopStart(Window,
  1788.                                 SampleObjectGetLoopStart2(Window->SampleObject));
  1789.                             SampleWindowPutLoopEnd(Window,
  1790.                                 SampleObjectGetLoopEnd2(Window->SampleObject));
  1791.                             break;
  1792.                         case eSampleLoop2:
  1793.                             /* switch from 2 to 2 -- no change */
  1794.                             break;
  1795.                         case eSampleLoop3:
  1796.                             /* put away 3 */
  1797.                             SampleObjectPutLoopStart3(Window->SampleObject,
  1798.                                 SampleWindowGetLoopStart(Window));
  1799.                             SampleObjectPutLoopEnd3(Window->SampleObject,
  1800.                                 SampleWindowGetLoopEnd(Window));
  1801.                             /* load 2 */
  1802.                             SampleWindowPutLoopStart(Window,
  1803.                                 SampleObjectGetLoopStart2(Window->SampleObject));
  1804.                             SampleWindowPutLoopEnd(Window,
  1805.                                 SampleObjectGetLoopEnd2(Window->SampleObject));
  1806.                             break;
  1807.                     }
  1808.                 Window->EditingWhichLoop = eSampleLoop2;
  1809.                 XSize = GetWindowWidth(Window->ScreenID);
  1810.                 YSize = GetWindowHeight(Window->ScreenID);
  1811.                 SetClipRect(Window->ScreenID,0,0,XSize,YSize);
  1812.                 DrawBoxErase(Window->ScreenID,0,0,XSize,YSize);
  1813.                 SampleWindowUpdator(Window);
  1814.             }
  1815.         else if (MenuItem == mSampleEditLoop3)
  1816.             {
  1817.                 OrdType                        XSize;
  1818.                 OrdType                        YSize;
  1819.  
  1820.                 SampleWindowFinalizeCurrentEdit(Window);
  1821.                 switch (Window->EditingWhichLoop)
  1822.                     {
  1823.                         default:
  1824.                             EXECUTE(PRERR(ForceAbort,
  1825.                                 "SampleWindowDoMenuCommand:  bad value in EditingWhichLoop"));
  1826.                             break;
  1827.                         case eSampleLoop1:
  1828.                             /* put away 1 */
  1829.                             SampleObjectPutLoopStart1(Window->SampleObject,
  1830.                                 SampleWindowGetLoopStart(Window));
  1831.                             SampleObjectPutLoopEnd1(Window->SampleObject,
  1832.                                 SampleWindowGetLoopEnd(Window));
  1833.                             /* load 3 */
  1834.                             SampleWindowPutLoopStart(Window,
  1835.                                 SampleObjectGetLoopStart3(Window->SampleObject));
  1836.                             SampleWindowPutLoopEnd(Window,
  1837.                                 SampleObjectGetLoopEnd3(Window->SampleObject));
  1838.                             break;
  1839.                         case eSampleLoop2:
  1840.                             /* put away 2 */
  1841.                             SampleObjectPutLoopStart2(Window->SampleObject,
  1842.                                 SampleWindowGetLoopStart(Window));
  1843.                             SampleObjectPutLoopEnd2(Window->SampleObject,
  1844.                                 SampleWindowGetLoopEnd(Window));
  1845.                             /* load 3 */
  1846.                             SampleWindowPutLoopStart(Window,
  1847.                                 SampleObjectGetLoopStart3(Window->SampleObject));
  1848.                             SampleWindowPutLoopEnd(Window,
  1849.                                 SampleObjectGetLoopEnd3(Window->SampleObject));
  1850.                             break;
  1851.                         case eSampleLoop3:
  1852.                             /* switch from 3 to 3 -- no change */
  1853.                             break;
  1854.                     }
  1855.                 Window->EditingWhichLoop = eSampleLoop3;
  1856.                 XSize = GetWindowWidth(Window->ScreenID);
  1857.                 YSize = GetWindowHeight(Window->ScreenID);
  1858.                 SetClipRect(Window->ScreenID,0,0,XSize,YSize);
  1859.                 DrawBoxErase(Window->ScreenID,0,0,XSize,YSize);
  1860.                 SampleWindowUpdator(Window);
  1861.             }
  1862.         else if (MenuItem == mFind)
  1863.             {
  1864.                 if (Window->ActiveTextEdit != Window->SampleExpressionEdit)
  1865.                     {
  1866.                         SampleWindowFinalizeCurrentEdit(Window);
  1867.                         Window->ActiveTextEdit = Window->SampleExpressionEdit;
  1868.                         EnableTextEditSelection(Window->ActiveTextEdit);
  1869.                     }
  1870.                 switch (DoFindDialog(&GlobalSearchString,&GlobalReplaceString,
  1871.                     mCut,mPaste,mCopy,mUndo,mSelectAll,mClear))
  1872.                     {
  1873.                         default:
  1874.                             EXECUTE(PRERR(ForceAbort,
  1875.                                 "SampleWindowDoMenuCommand:  bad value from DoFindDialog"));
  1876.                             break;
  1877.                         case eFindCancel:
  1878.                         case eDontFind:
  1879.                             break;
  1880.                         case eFindFromStart:
  1881.                             SetTextEditInsertionPoint(Window->ActiveTextEdit,0,0);
  1882.                             TextEditFindAgain(Window->ActiveTextEdit,GlobalSearchString);
  1883.                             TextEditShowSelection(Window->ActiveTextEdit);
  1884.                             break;
  1885.                         case eFindAgain:
  1886.                             TextEditFindAgain(Window->ActiveTextEdit,GlobalSearchString);
  1887.                             TextEditShowSelection(Window->ActiveTextEdit);
  1888.                             break;
  1889.                     }
  1890.             }
  1891.         else if (MenuItem == mFindAgain)
  1892.             {
  1893.                 if (Window->ActiveTextEdit != Window->SampleExpressionEdit)
  1894.                     {
  1895.                         SampleWindowFinalizeCurrentEdit(Window);
  1896.                         Window->ActiveTextEdit = Window->SampleExpressionEdit;
  1897.                         EnableTextEditSelection(Window->ActiveTextEdit);
  1898.                     }
  1899.                 TextEditFindAgain(Window->ActiveTextEdit,GlobalSearchString);
  1900.                 TextEditShowSelection(Window->ActiveTextEdit);
  1901.             }
  1902.         else if (MenuItem == mReplace)
  1903.             {
  1904.                 if (Window->ActiveTextEdit != Window->SampleExpressionEdit)
  1905.                     {
  1906.                         SampleWindowFinalizeCurrentEdit(Window);
  1907.                         Window->ActiveTextEdit = Window->SampleExpressionEdit;
  1908.                         EnableTextEditSelection(Window->ActiveTextEdit);
  1909.                     }
  1910.                 if (TextEditIsThereValidSelection(Window->ActiveTextEdit))
  1911.                     {
  1912.                         TextEditInsertRawDataWithUndo(Window->ActiveTextEdit,GlobalReplaceString,
  1913.                             SYSTEMLINEFEED);
  1914.                     }
  1915.             }
  1916.         else if (MenuItem == mReplaceAndFindAgain)
  1917.             {
  1918.                 if (Window->ActiveTextEdit != Window->SampleExpressionEdit)
  1919.                     {
  1920.                         SampleWindowFinalizeCurrentEdit(Window);
  1921.                         Window->ActiveTextEdit = Window->SampleExpressionEdit;
  1922.                         EnableTextEditSelection(Window->ActiveTextEdit);
  1923.                     }
  1924.                 if (TextEditIsThereValidSelection(Window->ActiveTextEdit))
  1925.                     {
  1926.                         TextEditInsertRawDataWithUndo(Window->ActiveTextEdit,GlobalReplaceString,
  1927.                             SYSTEMLINEFEED);
  1928.                         TextEditFindAgain(Window->ActiveTextEdit,GlobalSearchString);
  1929.                         TextEditShowSelection(Window->ActiveTextEdit);
  1930.                     }
  1931.             }
  1932.         else if (MenuItem == mEnterSelection)
  1933.             {
  1934.                 if (Window->ActiveTextEdit != NIL)
  1935.                     {
  1936.                         char*                        NewString;
  1937.  
  1938.                         NewString = TextEditGetSelection(Window->ActiveTextEdit);
  1939.                         if (NewString != NIL)
  1940.                             {
  1941.                                 ReleasePtr(GlobalSearchString);
  1942.                                 GlobalSearchString = NewString;
  1943.                             }
  1944.                     }
  1945.             }
  1946.         else if (MenuItem == mExportWAVFormat)
  1947.             {
  1948.                 SampleStorageActualRec*        TheStuff;
  1949.  
  1950.                 TheStuff = SampleWindowGetTheSampleActual(Window);
  1951.                 if (TheStuff != NIL)
  1952.                     {
  1953.                         ExportWAVSample(TheStuff,SampleWindowGetSamplingRate(Window));
  1954.                         DisposeSampleStorageActual(TheStuff);
  1955.                     }
  1956.                  else
  1957.                     {
  1958.                         AlertHalt("There is not enough memory available to export the sample.",NIL);
  1959.                     }
  1960.             }
  1961.         else if (MenuItem == mExportRAWFormat)
  1962.             {
  1963.                 SampleStorageActualRec*        TheStuff;
  1964.  
  1965.                 TheStuff = SampleWindowGetTheSampleActual(Window);
  1966.                 if (TheStuff != NIL)
  1967.                     {
  1968.                         ExportRAWSample(TheStuff);
  1969.                         DisposeSampleStorageActual(TheStuff);
  1970.                     }
  1971.                  else
  1972.                     {
  1973.                         AlertHalt("There is not enough memory available to export the sample.",NIL);
  1974.                     }
  1975.             }
  1976.         else if (MenuItem == mExportAIFFFormat)
  1977.             {
  1978.                 SampleStorageActualRec*        TheStuff;
  1979.  
  1980.                 TheStuff = SampleWindowGetTheSampleActual(Window);
  1981.                 if (TheStuff != NIL)
  1982.                     {
  1983.                         ExportAIFFSample(TheStuff,SampleWindowGetSamplingRate(Window));
  1984.                         DisposeSampleStorageActual(TheStuff);
  1985.                     }
  1986.                  else
  1987.                     {
  1988.                         AlertHalt("There is not enough memory available to export the sample.",NIL);
  1989.                     }
  1990.             }
  1991.         else
  1992.             {
  1993.                 EXECUTE(PRERR(AllowResume,"SampleWindowDoMenuCommand:  unknown menu command"));
  1994.             }
  1995.     }
  1996.  
  1997.  
  1998. /* copy data in the currently active text edit field to wherever it belongs */
  1999. void                                SampleWindowFinalizeCurrentEdit(SampleWindowRec* Window)
  2000.     {
  2001.         CheckPtrExistence(Window);
  2002.         if (Window->ActiveTextEdit != NIL)
  2003.             {
  2004.                 if (Window->ActiveTextEdit == Window->SampleExpressionEdit)
  2005.                     {
  2006.                     }
  2007.                 else if (Window->ActiveTextEdit == Window->NameEdit)
  2008.                     {
  2009.                     }
  2010.                 else if (Window->ActiveTextEdit == Window->SamplingRateEdit)
  2011.                     {
  2012.                         long                    Temp;
  2013.  
  2014.                         Temp = SampleWindowGetSamplingRate(Window);
  2015.                         if (Temp < MINSAMPLINGRATE)
  2016.                             {
  2017.                                 SampleWindowPutSamplingRate(Window,MINSAMPLINGRATE);
  2018.                             }
  2019.                         if (Temp > MAXSAMPLINGRATE)
  2020.                             {
  2021.                                 SampleWindowPutSamplingRate(Window,MAXSAMPLINGRATE);
  2022.                             }
  2023.                     }
  2024.                 else if (Window->ActiveTextEdit == Window->NaturalFrequencyEdit)
  2025.                     {
  2026.                         double                Temp;
  2027.  
  2028.                         Temp = SampleWindowGetNaturalFrequency(Window);
  2029.                         if (Temp < MINNATURALFREQ)
  2030.                             {
  2031.                                 SampleWindowPutNaturalFrequency(Window,MINNATURALFREQ);
  2032.                             }
  2033.                         if (Temp > MAXNATURALFREQ)
  2034.                             {
  2035.                                 SampleWindowPutNaturalFrequency(Window,MAXNATURALFREQ);
  2036.                             }
  2037.                     }
  2038.                 else if (Window->ActiveTextEdit == Window->OriginEdit)
  2039.                     {
  2040.                         AdjustSampleWindowThangs(Window);
  2041.                         SampleWindowUpdator(Window);
  2042.                     }
  2043.                 else if (Window->ActiveTextEdit == Window->LoopStartEdit)
  2044.                     {
  2045.                         AdjustSampleWindowThangs(Window);
  2046.                         SampleWindowUpdator(Window);
  2047.                     }
  2048.                 else if (Window->ActiveTextEdit == Window->LoopEndEdit)
  2049.                     {
  2050.                         AdjustSampleWindowThangs(Window);
  2051.                         SampleWindowUpdator(Window);
  2052.                     }
  2053.                 else if (Window->ActiveTextEdit == Window->SelectionStartEdit)
  2054.                     {
  2055.                         char*                            StringTemp;
  2056.  
  2057.                         StringTemp = TextEditGetRawData(Window->SelectionStartEdit,"\x0a");
  2058.                         if (StringTemp != NIL)
  2059.                             {
  2060.                                 SetSampleViewSelection(Window->SampleView,
  2061.                                     StringToInteger(StringTemp,PtrSize(StringTemp)),
  2062.                                     GetSampleViewSelectEnd(Window->SampleView));
  2063.                                 SampleWindowPutSelectStart(Window);
  2064.                                 SampleWindowPutSelectEnd(Window);
  2065.                                 SampleWindowRedrawThangs(Window);
  2066.                                 ReleasePtr(StringTemp);
  2067.                             }
  2068.                     }
  2069.                 else if (Window->ActiveTextEdit == Window->SelectionEndEdit)
  2070.                     {
  2071.                         char*                            StringTemp;
  2072.  
  2073.                         StringTemp = TextEditGetRawData(Window->SelectionEndEdit,"\x0a");
  2074.                         if (StringTemp != NIL)
  2075.                             {
  2076.                                 SetSampleViewSelection(Window->SampleView,
  2077.                                     GetSampleViewSelectStart(Window->SampleView),
  2078.                                     StringToInteger(StringTemp,PtrSize(StringTemp)));
  2079.                                 SampleWindowPutSelectStart(Window);
  2080.                                 SampleWindowPutSelectEnd(Window);
  2081.                                 SampleWindowRedrawThangs(Window);
  2082.                                 ReleasePtr(StringTemp);
  2083.                             }
  2084.                     }
  2085.                 else if (Window->ActiveTextEdit == Window->ScalingEdit)
  2086.                     {
  2087.                         char*                            StringTemp;
  2088.  
  2089.                         StringTemp = TextEditGetRawData(Window->ScalingEdit,"\x0a");
  2090.                         if (StringTemp != NIL)
  2091.                             {
  2092.                                 SetSampleViewHorizontalScale(Window->SampleView,
  2093.                                     StringToLongDouble(StringTemp,PtrSize(StringTemp)));
  2094.                                 SampleWindowPutScalingFactor(Window);
  2095.                                 SampleWindowRedrawThangs(Window);
  2096.                                 UpdateSampleWindowScrollbar(Window);
  2097.                                 ReleasePtr(StringTemp);
  2098.                             }
  2099.                     }
  2100.                 else
  2101.                     {
  2102.                         EXECUTE(PRERR(AllowResume,"SampleWindowFinalizeCurrentEdit:  "
  2103.                             "an unknown text edit is in the ActiveTextEdit field."));
  2104.                     }
  2105.                 DisableTextEditSelection(Window->ActiveTextEdit);
  2106.                 Window->ActiveTextEdit = NIL;
  2107.             }
  2108.     }
  2109.  
  2110.  
  2111. /* get the editing version of the origin point */
  2112. long                                SampleWindowGetOrigin(SampleWindowRec* Window)
  2113.     {
  2114.         long                            ReturnValue;
  2115.         char*                            StringTemp;
  2116.  
  2117.         CheckPtrExistence(Window);
  2118.         StringTemp = TextEditGetRawData(Window->OriginEdit,"\x0a");
  2119.         if (StringTemp != NIL)
  2120.             {
  2121.                 ReturnValue = StringToInteger(StringTemp,PtrSize(StringTemp));
  2122.                 ReleasePtr(StringTemp);
  2123.             }
  2124.          else
  2125.             {
  2126.                 ReturnValue = 0;
  2127.             }
  2128.         return ReturnValue;
  2129.     }
  2130.  
  2131.  
  2132. /* get the current loop start point */
  2133. long                                SampleWindowGetLoopStart(SampleWindowRec* Window)
  2134.     {
  2135.         long                            ReturnValue;
  2136.         char*                            StringTemp;
  2137.  
  2138.         CheckPtrExistence(Window);
  2139.         StringTemp = TextEditGetRawData(Window->LoopStartEdit,"\x0a");
  2140.         if (StringTemp != NIL)
  2141.             {
  2142.                 ReturnValue = StringToInteger(StringTemp,PtrSize(StringTemp));
  2143.                 ReleasePtr(StringTemp);
  2144.             }
  2145.          else
  2146.             {
  2147.                 ReturnValue = 0;
  2148.             }
  2149.         return ReturnValue;
  2150.     }
  2151.  
  2152.  
  2153. /* get the current loop end point */
  2154. long                                SampleWindowGetLoopEnd(SampleWindowRec* Window)
  2155.     {
  2156.         long                            ReturnValue;
  2157.         char*                            StringTemp;
  2158.  
  2159.         CheckPtrExistence(Window);
  2160.         StringTemp = TextEditGetRawData(Window->LoopEndEdit,"\x0a");
  2161.         if (StringTemp != NIL)
  2162.             {
  2163.                 ReturnValue = StringToInteger(StringTemp,PtrSize(StringTemp));
  2164.                 ReleasePtr(StringTemp);
  2165.             }
  2166.          else
  2167.             {
  2168.                 ReturnValue = 0;
  2169.             }
  2170.         return ReturnValue;
  2171.     }
  2172.  
  2173.  
  2174. /* get the sampling rate */
  2175. long                                SampleWindowGetSamplingRate(SampleWindowRec* Window)
  2176.     {
  2177.         long                            ReturnValue;
  2178.         char*                            StringTemp;
  2179.  
  2180.         CheckPtrExistence(Window);
  2181.         StringTemp = TextEditGetRawData(Window->SamplingRateEdit,"\x0a");
  2182.         if (StringTemp != NIL)
  2183.             {
  2184.                 ReturnValue = StringToInteger(StringTemp,PtrSize(StringTemp));
  2185.                 ReleasePtr(StringTemp);
  2186.             }
  2187.          else
  2188.             {
  2189.                 ReturnValue = 0;
  2190.             }
  2191.         return ReturnValue;
  2192.     }
  2193.  
  2194.  
  2195. /* get the natural recorded pitch */
  2196. double                            SampleWindowGetNaturalFrequency(SampleWindowRec* Window)
  2197.     {
  2198.         double                        ReturnValue;
  2199.         char*                            StringTemp;
  2200.  
  2201.         CheckPtrExistence(Window);
  2202.         StringTemp = TextEditGetRawData(Window->NaturalFrequencyEdit,"\x0a");
  2203.         if (StringTemp != NIL)
  2204.             {
  2205.                 ReturnValue = StringToLongDouble(StringTemp,PtrSize(StringTemp));
  2206.                 ReleasePtr(StringTemp);
  2207.             }
  2208.          else
  2209.             {
  2210.                 ReturnValue = 0;
  2211.             }
  2212.         return ReturnValue;
  2213.     }
  2214.  
  2215.  
  2216. /* copy the select start point from the sample view to the edit box */
  2217. void                                SampleWindowPutSelectStart(SampleWindowRec* Window)
  2218.     {
  2219.         char*                            StringTemp;
  2220.  
  2221.         CheckPtrExistence(Window);
  2222.         StringTemp = IntegerToString(GetSampleViewSelectStart(Window->SampleView));
  2223.         if (StringTemp != NIL)
  2224.             {
  2225.                 TextEditNewRawData(Window->SelectionStartEdit,StringTemp,"\x0a");
  2226.                 ReleasePtr(StringTemp);
  2227.             }
  2228.     }
  2229.  
  2230.  
  2231. /* copy the select end point from the sample view to the edit box */
  2232. void                                SampleWindowPutSelectEnd(SampleWindowRec* Window)
  2233.     {
  2234.         char*                            StringTemp;
  2235.  
  2236.         CheckPtrExistence(Window);
  2237.         StringTemp = IntegerToString(GetSampleViewSelectEnd(Window->SampleView));
  2238.         if (StringTemp != NIL)
  2239.             {
  2240.                 TextEditNewRawData(Window->SelectionEndEdit,StringTemp,"\x0a");
  2241.                 ReleasePtr(StringTemp);
  2242.             }
  2243.     }
  2244.  
  2245.  
  2246. /* copy the scaling factor from the sample view to the edit box */
  2247. void                                SampleWindowPutScalingFactor(SampleWindowRec* Window)
  2248.     {
  2249.         char*                            StringTemp;
  2250.  
  2251.         CheckPtrExistence(Window);
  2252.         StringTemp = LongDoubleToString(
  2253.             GetSampleViewHorizontalScale(Window->SampleView),10,1e-4,1e6);
  2254.         if (StringTemp != NIL)
  2255.             {
  2256.                 TextEditNewRawData(Window->ScalingEdit,StringTemp,"\x0a");
  2257.                 ReleasePtr(StringTemp);
  2258.             }
  2259.     }
  2260.  
  2261.  
  2262. /* put a new origin point into the edit box */
  2263. void                                SampleWindowPutOrigin(SampleWindowRec* Window, long NewOrigin)
  2264.     {
  2265.         char*                            StringTemp;
  2266.  
  2267.         CheckPtrExistence(Window);
  2268.         SampleWindowRedrawUnderThangs(Window);
  2269.         StringTemp = IntegerToString(NewOrigin);
  2270.         if (StringTemp != NIL)
  2271.             {
  2272.                 TextEditNewRawData(Window->OriginEdit,StringTemp,"\x0a");
  2273.                 ReleasePtr(StringTemp);
  2274.                 AdjustSampleWindowThangs(Window);
  2275.                 SampleWindowRedrawThangs(Window);
  2276.             }
  2277.     }
  2278.  
  2279.  
  2280. /* put a new loop start into the current loop start edit box */
  2281. void                                SampleWindowPutLoopStart(SampleWindowRec* Window, long NewLoopStart)
  2282.     {
  2283.         char*                            StringTemp;
  2284.  
  2285.         CheckPtrExistence(Window);
  2286.         SampleWindowRedrawUnderThangs(Window);
  2287.         StringTemp = IntegerToString(NewLoopStart);
  2288.         if (StringTemp != NIL)
  2289.             {
  2290.                 TextEditNewRawData(Window->LoopStartEdit,StringTemp,"\x0a");
  2291.                 ReleasePtr(StringTemp);
  2292.                 AdjustSampleWindowThangs(Window);
  2293.                 SampleWindowRedrawThangs(Window);
  2294.             }
  2295.     }
  2296.  
  2297.  
  2298. /* put a new loop end into the current loop end edit box */
  2299. void                                SampleWindowPutLoopEnd(SampleWindowRec* Window, long NewLoopEnd)
  2300.     {
  2301.         char*                            StringTemp;
  2302.  
  2303.         CheckPtrExistence(Window);
  2304.         SampleWindowRedrawUnderThangs(Window);
  2305.         StringTemp = IntegerToString(NewLoopEnd);
  2306.         if (StringTemp != NIL)
  2307.             {
  2308.                 TextEditNewRawData(Window->LoopEndEdit,StringTemp,"\x0a");
  2309.                 ReleasePtr(StringTemp);
  2310.                 AdjustSampleWindowThangs(Window);
  2311.                 SampleWindowRedrawThangs(Window);
  2312.             }
  2313.     }
  2314.  
  2315.  
  2316. /* put a new sampling rate into the sampling rate edit box */
  2317. void                                SampleWindowPutSamplingRate(SampleWindowRec* Window,
  2318.                                             long NewSamplingRate)
  2319.     {
  2320.         char*                            StringTemp;
  2321.  
  2322.         CheckPtrExistence(Window);
  2323.         StringTemp = IntegerToString(NewSamplingRate);
  2324.         if (StringTemp != NIL)
  2325.             {
  2326.                 TextEditNewRawData(Window->SamplingRateEdit,StringTemp,"\x0a");
  2327.                 ReleasePtr(StringTemp);
  2328.             }
  2329.     }
  2330.  
  2331.  
  2332. /* put a new natural recorded pitch into the natural frequency edit box */
  2333. void                                SampleWindowPutNaturalFrequency(SampleWindowRec* Window,
  2334.                                             double NewNaturalFrequency)
  2335.     {
  2336.         char*                            StringTemp;
  2337.  
  2338.         CheckPtrExistence(Window);
  2339.         StringTemp = LongDoubleToString(NewNaturalFrequency,13,1e-4,1e6);
  2340.         if (StringTemp != NIL)
  2341.             {
  2342.                 TextEditNewRawData(Window->NaturalFrequencyEdit,StringTemp,"\x0a");
  2343.                 ReleasePtr(StringTemp);
  2344.             }
  2345.     }
  2346.  
  2347.  
  2348. /* find the current mouse position and put it in the box */
  2349. void                                SampleWindowPutMousePosition(SampleWindowRec* Window)
  2350.     {
  2351.         char*                            StringTemp;
  2352.         OrdType                        MouseXPosition;
  2353.         OrdType                        StupidYPosition;
  2354.         long                            CalculatedOffset;
  2355.  
  2356.         CheckPtrExistence(Window);
  2357.         GetMousePosition(&MouseXPosition,&StupidYPosition);
  2358.         CalculatedOffset = ((MouseXPosition - 1 - GetSampleViewXLoc(Window->SampleView))
  2359.             * GetSampleViewHorizontalScale(Window->SampleView))
  2360.             + GetSampleViewXOffset(Window->SampleView);
  2361.         StringTemp = IntegerToString(CalculatedOffset);
  2362.         if (StringTemp != NIL)
  2363.             {
  2364.                 TextEditNewRawData(Window->MousePositionEdit,StringTemp,"\x0a");
  2365.                 ReleasePtr(StringTemp);
  2366.             }
  2367.     }
  2368.  
  2369.  
  2370. /* get the number of bits used to store the sample */
  2371. NumBitsType                    SampleWindowGetNumBits(SampleWindowRec* Window)
  2372.     {
  2373.         CheckPtrExistence(Window);
  2374.         return GetSampleViewNumBits(Window->SampleView);
  2375.     }
  2376.  
  2377.  
  2378. /* get the number of channels the sample has */
  2379. NumChannelsType            SampleWindowGetNumChannels(SampleWindowRec* Window)
  2380.     {
  2381.         CheckPtrExistence(Window);
  2382.         return GetSampleViewNumChannels(Window->SampleView);
  2383.     }
  2384.  
  2385.  
  2386. /* get a copy of the name of the sample */
  2387. char*                                SampleWindowGetNameCopy(SampleWindowRec* Window)
  2388.     {
  2389.         char*                            ReturnValue;
  2390.  
  2391.         CheckPtrExistence(Window);
  2392.         ReturnValue = TextEditGetRawData(Window->NameEdit,"\x0a");
  2393.         if (ReturnValue != NIL)
  2394.             {
  2395.                 SetTag(ReturnValue,"SampWindowNameCopy");
  2396.             }
  2397.         return ReturnValue;
  2398.     }
  2399.  
  2400.  
  2401. /* get the number of frames in the current sample */
  2402. long                                SampleWindowGetNumFrames(SampleWindowRec* Window)
  2403.     {
  2404.         CheckPtrExistence(Window);
  2405.         return GetSampleViewNumFrames(Window->SampleView);
  2406.     }
  2407.  
  2408.  
  2409. /* get a copy of the formula text */
  2410. char*                                SampleWindowGetFormulaCopy(SampleWindowRec* Window)
  2411.     {
  2412.         char*                            TextCopy;
  2413.  
  2414.         CheckPtrExistence(Window);
  2415.         TextCopy = TextEditGetRawData(Window->SampleExpressionEdit,"\x0a");
  2416.         if (TextCopy != NIL)
  2417.             {
  2418.                 SetTag(TextCopy,"SampWinFormulaCopy");
  2419.             }
  2420.         return TextCopy;
  2421.     }
  2422.  
  2423.  
  2424. /* get a copy of the left channel for a stereo sample. */
  2425. largefixedsigned*        SampleWindowGetFixedArrayLeft(SampleWindowRec* Window)
  2426.     {
  2427.         CheckPtrExistence(Window);
  2428.         return SampleViewGetChannelFixed(Window->SampleView,eLeftChannel);
  2429.     }
  2430.  
  2431.  
  2432. /* get a copy of the right channel for a stereo sample. */
  2433. largefixedsigned*        SampleWindowGetFixedArrayRight(SampleWindowRec* Window)
  2434.     {
  2435.         CheckPtrExistence(Window);
  2436.         return SampleViewGetChannelFixed(Window->SampleView,eRightChannel);
  2437.     }
  2438.  
  2439.  
  2440. /* get a copy of a mono sample. */
  2441. largefixedsigned*        SampleWindowGetFixedArrayMono(SampleWindowRec* Window)
  2442.     {
  2443.         CheckPtrExistence(Window);
  2444.         return SampleViewGetChannelFixed(Window->SampleView,eMonoChannel);
  2445.     }
  2446.  
  2447.  
  2448. /* put a new name in.  the caller is responsible for deleting NewName */
  2449. void                                SampleWindowObjectNameChange(SampleWindowRec* Window, char* NewName)
  2450.     {
  2451.         CheckPtrExistence(Window);
  2452.         CheckPtrExistence(NewName);
  2453.         /* we only use the name, but we don't dispose it */
  2454.         TextEditNewRawData(Window->NameEdit,NewName,"\x0a");
  2455.         TextEditHasBeenSaved(Window->NameEdit);
  2456.     }
  2457.  
  2458.  
  2459. /* this is the argument list for stereo sample */
  2460. static FunctionParamRec        StereoArgList[] =
  2461.     {
  2462.         {"loopstart",eInteger},
  2463.         {"loopend",eInteger},
  2464.         {"origin",eInteger},
  2465.         {"samplingrate",eInteger},
  2466.         {"naturalfrequency",eDouble},
  2467.         {"selectstart",eInteger},
  2468.         {"selectend",eInteger},
  2469.         {"leftdata",eArrayOfFixed},
  2470.         {"rightdata",eArrayOfFixed}
  2471.     };
  2472. #define STEREOARGLISTLENGTH (sizeof(StereoArgList) / sizeof(StereoArgList[0]))
  2473.  
  2474.  
  2475. /* this is the argument list for mono sample */
  2476. static FunctionParamRec        MonoArgList[] =
  2477.     {
  2478.         {"loopstart",eInteger},
  2479.         {"loopend",eInteger},
  2480.         {"origin",eInteger},
  2481.         {"samplingrate",eInteger},
  2482.         {"naturalfrequency",eDouble},
  2483.         {"selectstart",eInteger},
  2484.         {"selectend",eInteger},
  2485.         {"data",eArrayOfFixed}
  2486.     };
  2487. #define MONOARGLISTLENGTH (sizeof(MonoArgList) / sizeof(MonoArgList[0]))
  2488.  
  2489.  
  2490. /* evaluate the formula and modify the sample */
  2491. void                                SampleWindowPerformCalculation(SampleWindowRec* Window)
  2492.     {
  2493.         char*                                Blob;
  2494.         PcodeRec*                        FuncCode;
  2495.         CompileErrors                Error;
  2496.         long                                LineNumber;
  2497.         ParamStackRec*            ParamList;
  2498.         EvalErrors                    OtherError;
  2499.         OpcodeRec*                    ErrorOpcode;
  2500.         long                                OffendingInstruction;
  2501.         DataTypes                        ReturnType;
  2502.  
  2503.         CheckPtrExistence(Window);
  2504.  
  2505.         /* bring the world up to date */
  2506.         if (!MainWindowMakeUpToDateFunctions(Window->MainWindow))
  2507.             {
  2508.                 return;
  2509.             }
  2510.  
  2511.         /* prepare the text blob to be evaluated */
  2512.         Blob = TextEditGetRawData(Window->SampleExpressionEdit,"\x0a");
  2513.         if (Blob == NIL)
  2514.             {
  2515.              FailurePoint1:
  2516.                 AlertHalt("There is not enough memory available to compile the expression.",NIL);
  2517.                 return;
  2518.             }
  2519.  
  2520.         /* perform compilation */
  2521.         Error = CompileSpecialFunction((GetSampleViewNumChannels(Window->SampleView)
  2522.             == eSampleStereo) ? StereoArgList : MonoArgList,(GetSampleViewNumChannels(
  2523.             Window->SampleView) == eSampleStereo) ? STEREOARGLISTLENGTH : MONOARGLISTLENGTH,
  2524.             &LineNumber,&ReturnType,Blob,&FuncCode);
  2525.         ReleasePtr(Blob);
  2526.         if (Error != eCompileNoError)
  2527.             {
  2528.                 SetTextEditSelection(Window->SampleExpressionEdit,LineNumber - 1,0,LineNumber,0);
  2529.                 TextEditShowSelection(Window->SampleExpressionEdit);
  2530.                 AlertHalt("A compile error occurred:  _",GetCompileErrorString(Error));
  2531.                 return;
  2532.             }
  2533.  
  2534.         /* try to evaluate the code */
  2535.         ParamList = NewParamStack();
  2536.         if (ParamList == NIL)
  2537.             {
  2538.              SecondFailurePoint1:
  2539.                 DisposePcode(FuncCode);
  2540.                 AlertHalt("There is not enough memory available to evaluate the expression.",NIL);
  2541.                 return;
  2542.             }
  2543.         /* add a space for the return value */
  2544.         if (!AddIntegerToStack(ParamList,0))
  2545.             {
  2546.              SecondFailurePoint2:
  2547.                 DisposeParamStack(ParamList);
  2548.                 goto SecondFailurePoint1;
  2549.             }
  2550.         /* add the special parameters (SymbolStack order MUST be the same */
  2551.         /* order as that used for the Parameterlist) */
  2552.         /* loopstart */
  2553.         if (!AddIntegerToStack(ParamList,SampleWindowGetLoopStart(Window)))
  2554.             {
  2555.                 goto SecondFailurePoint2;
  2556.             }
  2557.         /* loopend */
  2558.         if (!AddIntegerToStack(ParamList,SampleWindowGetLoopEnd(Window)))
  2559.             {
  2560.                 goto SecondFailurePoint2;
  2561.             }
  2562.         /* origin */
  2563.         if (!AddIntegerToStack(ParamList,SampleWindowGetOrigin(Window)))
  2564.             {
  2565.                 goto SecondFailurePoint2;
  2566.             }
  2567.         /* samplingrate */
  2568.         if (!AddIntegerToStack(ParamList,SampleWindowGetSamplingRate(Window)))
  2569.             {
  2570.                 goto SecondFailurePoint2;
  2571.             }
  2572.         /* basefrequency */
  2573.         if (!AddDoubleToStack(ParamList,SampleWindowGetNaturalFrequency(Window)))
  2574.             {
  2575.                 goto SecondFailurePoint2;
  2576.             }
  2577.         /* selectstart */
  2578.         if (!AddIntegerToStack(ParamList,GetSampleViewSelectStart(Window->SampleView)))
  2579.             {
  2580.                 goto SecondFailurePoint2;
  2581.             }
  2582.         /* selectend */
  2583.         if (!AddIntegerToStack(ParamList,GetSampleViewSelectEnd(Window->SampleView)))
  2584.             {
  2585.                 goto SecondFailurePoint2;
  2586.             }
  2587.         /* data (the actual thing is added, so we don't have to dispose it!) */
  2588.         if (GetSampleViewNumChannels(Window->SampleView) == eSampleStereo)
  2589.             {
  2590.                 largefixedsigned*        DataBlock;
  2591.  
  2592.                 /* left channel */
  2593.                 DataBlock = SampleViewGetChannelFixed(Window->SampleView,eLeftChannel);
  2594.                 if (DataBlock == NIL)
  2595.                     {
  2596.                         goto SecondFailurePoint2;
  2597.                     }
  2598.                 if (!AddArrayToStack(ParamList,DataBlock))
  2599.                     {
  2600.                         ReleasePtr((char*)DataBlock);
  2601.                         goto SecondFailurePoint2;
  2602.                     }
  2603.                 /* right channel */
  2604.                 DataBlock = SampleViewGetChannelFixed(Window->SampleView,eRightChannel);
  2605.                 if (DataBlock == NIL)
  2606.                     {
  2607.                         goto SecondFailurePoint2;
  2608.                     }
  2609.                 if (!AddArrayToStack(ParamList,DataBlock))
  2610.                     {
  2611.                         ReleasePtr((char*)DataBlock);
  2612.                         goto SecondFailurePoint2;
  2613.                     }
  2614.             }
  2615.          else
  2616.             {
  2617.                 largefixedsigned*        DataBlock;
  2618.  
  2619.                 DataBlock = SampleViewGetChannelFixed(Window->SampleView,eMonoChannel);
  2620.                 if (DataBlock == NIL)
  2621.                     {
  2622.                         goto SecondFailurePoint2;
  2623.                     }
  2624.                 if (!AddArrayToStack(ParamList,DataBlock))
  2625.                     {
  2626.                         ReleasePtr((char*)DataBlock);
  2627.                         goto SecondFailurePoint2;
  2628.                     }
  2629.             }
  2630.  
  2631.         /* executing the actual code */
  2632.         OtherError = EvaluatePcode(ParamList,FuncCode,
  2633.             Window->CodeCenter,&ErrorOpcode,&OffendingInstruction,Window->MainWindow,
  2634.             (SampleErrors (*)(void*,char*,largefixedsigned**))&MainWindowGetSampleLeftCopy,
  2635.             (SampleErrors (*)(void*,char*,largefixedsigned**))&MainWindowGetSampleRightCopy,
  2636.             (SampleErrors (*)(void*,char*,largefixedsigned**))&MainWindowGetSampleMonoCopy,
  2637.             (SampleErrors (*)(void*,char*,long*))&MainWindowGetWaveTableFrameCount,
  2638.             (SampleErrors (*)(void*,char*,long*))&MainWindowGetWaveTableTableCount,
  2639.             (SampleErrors (*)(void*,char*,largefixedsigned**))&MainWindowGetWaveTableArray,
  2640.             Window->MainWindow,
  2641.             (struct InteractionWindowRec* (*)(void*))&MainWindowGetInteractionWindow);
  2642.         if (OtherError != eEvalNoError)
  2643.             {
  2644.                 char*                    FuncNameString;
  2645.                 FuncCodeRec*    ErrorFunction;
  2646.                 MyBoolean            SuccessFlag;
  2647.  
  2648.                 /* present error message */
  2649.                 SuccessFlag = False;
  2650.                 ErrorFunction = GetFunctionFromOpcode(Window->CodeCenter,ErrorOpcode);
  2651.                 if (ErrorFunction == NIL)
  2652.                     {
  2653.                         FuncNameString = StringToBlockCopy("<anonymous>");
  2654.                     }
  2655.                  else
  2656.                     {
  2657.                         FuncNameString = CopyPtr(GetFunctionName(ErrorFunction));
  2658.                     }
  2659.                 if (FuncNameString != NIL)
  2660.                     {
  2661.                         char*                    Key;
  2662.  
  2663.                         Key = StringToBlockCopy("_");
  2664.                         if (Key != NIL)
  2665.                             {
  2666.                                 char*                    BaseMessage;
  2667.  
  2668.                                 BaseMessage = StringFromRaw("Error in function _, instruction _:  _");
  2669.                                 if (BaseMessage != NIL)
  2670.                                     {
  2671.                                         char*                    FixedMessage1;
  2672.  
  2673.                                         FixedMessage1 = ReplaceBlockCopy(BaseMessage,Key,FuncNameString);
  2674.                                         if (FixedMessage1 != NIL)
  2675.                                             {
  2676.                                                 char*                    NumberStr;
  2677.  
  2678.                                                 NumberStr = IntegerToString(OffendingInstruction);
  2679.                                                 if (NumberStr != NIL)
  2680.                                                     {
  2681.                                                         char*                    FixedMessage2;
  2682.  
  2683.                                                         FixedMessage2 = ReplaceBlockCopy(FixedMessage1,Key,NumberStr);
  2684.                                                         if (FixedMessage2 != NIL)
  2685.                                                             {
  2686.                                                                 AlertHalt(FixedMessage2,GetPcodeErrorMessage(OtherError));
  2687.                                                                 SuccessFlag = True;
  2688.                                                                 ReleasePtr(FixedMessage2);
  2689.                                                             }
  2690.                                                         ReleasePtr(NumberStr);
  2691.                                                     }
  2692.                                                 ReleasePtr(FixedMessage1);
  2693.                                             }
  2694.                                         ReleasePtr(BaseMessage);
  2695.                                     }
  2696.                                 ReleasePtr(Key);
  2697.                             }
  2698.                         ReleasePtr(FuncNameString);
  2699.                     }
  2700.                 if (!SuccessFlag)
  2701.                     {
  2702.                         AlertHalt("There is not enough memory available to show "
  2703.                             "the compile error message.",NIL);
  2704.                     }
  2705.                 DisposeParamStack(ParamList);
  2706.                 DisposePcode(FuncCode);
  2707.                 return;
  2708.             }
  2709.  
  2710.         /* add the new data */
  2711.         if (GetSampleViewNumChannels(Window->SampleView) == eSampleStereo)
  2712.             {
  2713.                 largefixedsigned*        Left;
  2714.                 largefixedsigned*        Right;
  2715.  
  2716.                 Left = (largefixedsigned*)GetStackArray(ParamList,8);
  2717.                 Right = (largefixedsigned*)GetStackArray(ParamList,9);
  2718.                 if ((Left == NIL) || (Right == NIL))
  2719.                     {
  2720.                         AlertHalt("NIL array returned; data not changed.",NIL);
  2721.                     }
  2722.                 else if (PtrSize((char*)Left) != PtrSize((char*)Right))
  2723.                     {
  2724.                         AlertHalt("Left and Right arrays are not the same size.",NIL);
  2725.                     }
  2726.                 else
  2727.                     {
  2728.                         ERROR(PtrSize((char*)Left) % sizeof(largefixedsigned) != 0,
  2729.                             PRERR(ForceAbort,"SampleWindowPerformCalculation:  array alignment error"));
  2730.                         if (!SampleViewPutStereoFixed(Window->SampleView,Left,Right))
  2731.                             {
  2732.                                 AlertHalt("There is not enough memory available to "
  2733.                                     "write data back to editor.",NIL);
  2734.                             }
  2735.                     }
  2736.             }
  2737.          else
  2738.             {
  2739.                 largefixedsigned*        Middle;
  2740.  
  2741.                 Middle = (largefixedsigned*)GetStackArray(ParamList,8);
  2742.                 if (Middle == NIL)
  2743.                     {
  2744.                         AlertHalt("NIL array returned; data not changed.",NIL);
  2745.                     }
  2746.                 else
  2747.                     {
  2748.                         ERROR(PtrSize((char*)Middle) % sizeof(largefixedsigned) != 0,
  2749.                             PRERR(ForceAbort,"SampleWindowPerformCalculation:  array alignment error"));
  2750.                         if (!SampleViewPutMonoFixed(Window->SampleView,Middle))
  2751.                             {
  2752.                                 AlertHalt("There is not enough memory available to "
  2753.                                     "write data back to editor.",NIL);
  2754.                             }
  2755.                     }
  2756.             }
  2757.         DisposeParamStack(ParamList);
  2758.         DisposePcode(FuncCode);
  2759.         SampleWindowPutSelectStart(Window);
  2760.         SampleWindowPutSelectEnd(Window);
  2761.         AdjustSampleWindowThangs(Window);
  2762.         UpdateSampleWindowScrollbar(Window);
  2763.     }
  2764.  
  2765.  
  2766. /* redraw the origin and loop point bars */
  2767. void                                SampleWindowRedrawThangs(SampleWindowRec* Window)
  2768.     {
  2769.         long                            Location;
  2770.         OrdType                        XSize;
  2771.         OrdType                        YSize;
  2772.  
  2773.         CheckPtrExistence(Window);
  2774.         XSize = GetWindowWidth(Window->ScreenID);
  2775.         YSize = GetWindowHeight(Window->ScreenID);
  2776.  
  2777.         /* draw the cute horizontal bars */
  2778.         SetClipRect(Window->ScreenID,SAMPLEEDITX,SAMPLEEDITY,SAMPLEEDITWIDTH(XSize),
  2779.             SAMPLEEDITHEIGHT(YSize) - 15);
  2780.         DrawBoxFrame(Window->ScreenID,eBlack,SAMPLEEDITX,SAMPLEEDITY,
  2781.             SAMPLEEDITWIDTH(XSize),OVERLINEHEIGHT + 1);
  2782.         DrawBoxFrame(Window->ScreenID,eBlack,SAMPLEEDITX,SAMPLEEDITY + OVERLINEHEIGHT,
  2783.             SAMPLEEDITWIDTH(XSize),OVERLINEHEIGHT + 1);
  2784.         DrawBoxFrame(Window->ScreenID,eBlack,SAMPLEEDITX,SAMPLEEDITY + (2 * OVERLINEHEIGHT),
  2785.             SAMPLEEDITWIDTH(XSize),OVERLINEHEIGHT + 1);
  2786.         DrawBoxErase(Window->ScreenID,SAMPLEEDITX + 1,SAMPLEEDITY + 1,
  2787.             SAMPLEEDITWIDTH(XSize) - 2,OVERLINEHEIGHT + 1 - 2);
  2788.         DrawBoxErase(Window->ScreenID,SAMPLEEDITX + 1,SAMPLEEDITY + OVERLINEHEIGHT + 1,
  2789.             SAMPLEEDITWIDTH(XSize) - 2,OVERLINEHEIGHT + 1 - 2);
  2790.         DrawBoxErase(Window->ScreenID,SAMPLEEDITX + 1,SAMPLEEDITY + (2 * OVERLINEHEIGHT) + 1,
  2791.             SAMPLEEDITWIDTH(XSize) - 2,OVERLINEHEIGHT + 1 - 2);
  2792.         DrawTextLine(Window->ScreenID,GetScreenFont(),9,"Origin",6,SAMPLEEDITX + 5,
  2793.             SAMPLEEDITY + 1,ePlain);
  2794.         DrawTextLine(Window->ScreenID,GetScreenFont(),9,"Loop Start",10,SAMPLEEDITX + 5,
  2795.             SAMPLEEDITY + 1 + OVERLINEHEIGHT,ePlain);
  2796.         DrawTextLine(Window->ScreenID,GetScreenFont(),9,"Loop End",8,SAMPLEEDITX + 5,
  2797.             SAMPLEEDITY + 1 + (2 * OVERLINEHEIGHT),ePlain);
  2798.  
  2799.         /* drawing origin thang */
  2800.         Location = (SampleWindowGetOrigin(Window)
  2801.             - GetSampleViewXOffset(Window->SampleView))
  2802.             / GetSampleViewHorizontalScale(Window->SampleView);
  2803.         if ((Location > -16384) && (Location < 16384))
  2804.             {
  2805.                 DrawTrianglePaint(Window->ScreenID,eBlack,
  2806.                     SAMPLEEDITX - 4 + Location + 1,
  2807.                     SAMPLEEDITY + 1,
  2808.                     SAMPLEEDITX + 4 + Location + 1,
  2809.                     SAMPLEEDITY + 1,
  2810.                     SAMPLEEDITX + Location + 1,
  2811.                     SAMPLEEDITY + 8);
  2812.                 DrawLine(Window->ScreenID,eBlack,
  2813.                     SAMPLEEDITX + Location + 1,
  2814.                     SAMPLEEDITY + 1,
  2815.                     0,SAMPLEEDITHEIGHT(YSize) - 15);
  2816.             }
  2817.  
  2818.         /* drawing loop start thang */
  2819.         Location = (SampleWindowGetLoopStart(Window)
  2820.             - GetSampleViewXOffset(Window->SampleView))
  2821.             / GetSampleViewHorizontalScale(Window->SampleView);
  2822.         if ((Location > -16384) && (Location < 16384))
  2823.             {
  2824.                 DrawTrianglePaint(Window->ScreenID,eBlack,
  2825.                     SAMPLEEDITX - 4 + Location + 1,
  2826.                     SAMPLEEDITY + 1 + OVERLINEHEIGHT,
  2827.                     SAMPLEEDITX + 4 + Location + 1,
  2828.                     SAMPLEEDITY + 1 + OVERLINEHEIGHT,
  2829.                     SAMPLEEDITX + Location + 1,
  2830.                     SAMPLEEDITY + 8 + OVERLINEHEIGHT);
  2831.                 DrawLine(Window->ScreenID,eBlack,
  2832.                     SAMPLEEDITX + Location + 1,
  2833.                     SAMPLEEDITY + 1 + OVERLINEHEIGHT,
  2834.                     0,SAMPLEEDITHEIGHT(YSize) - 15 - OVERLINEHEIGHT);
  2835.             }
  2836.  
  2837.         /* drawing loop end thang */
  2838.         Location = (SampleWindowGetLoopEnd(Window)
  2839.             - GetSampleViewXOffset(Window->SampleView))
  2840.             / GetSampleViewHorizontalScale(Window->SampleView);
  2841.         if ((Location > -16384) && (Location < 16384))
  2842.             {
  2843.                 DrawTrianglePaint(Window->ScreenID,eBlack,
  2844.                     SAMPLEEDITX - 4 + Location + 1,
  2845.                     SAMPLEEDITY + 1 + (2 * OVERLINEHEIGHT),
  2846.                     SAMPLEEDITX + 4 + Location + 1,
  2847.                     SAMPLEEDITY + 1 + (2 * OVERLINEHEIGHT),
  2848.                     SAMPLEEDITX + Location + 1,
  2849.                     SAMPLEEDITY + 8 + (2 * OVERLINEHEIGHT));
  2850.                 DrawLine(Window->ScreenID,eBlack,
  2851.                     SAMPLEEDITX + Location + 1,
  2852.                     SAMPLEEDITY + 1 + (2 * OVERLINEHEIGHT),
  2853.                     0,SAMPLEEDITHEIGHT(YSize) - 15 - (2 * OVERLINEHEIGHT));
  2854.             }
  2855.     }
  2856.  
  2857.  
  2858. /* redraw the sample points that occur under the origin and loop point bars */
  2859. void                                SampleWindowRedrawUnderThangs(SampleWindowRec* Window)
  2860.     {
  2861.         long                            Location;
  2862.  
  2863.         CheckPtrExistence(Window);
  2864.         Location = (SampleWindowGetOrigin(Window)
  2865.             - GetSampleViewXOffset(Window->SampleView))
  2866.             / GetSampleViewHorizontalScale(Window->SampleView);
  2867.         RedrawSampleViewOneLine(Window->SampleView,Location + 1);
  2868.         Location = (SampleWindowGetLoopStart(Window)
  2869.             - GetSampleViewXOffset(Window->SampleView))
  2870.             / GetSampleViewHorizontalScale(Window->SampleView);
  2871.         RedrawSampleViewOneLine(Window->SampleView,Location + 1);
  2872.         Location = (SampleWindowGetLoopEnd(Window)
  2873.             - GetSampleViewXOffset(Window->SampleView))
  2874.             / GetSampleViewHorizontalScale(Window->SampleView);
  2875.         RedrawSampleViewOneLine(Window->SampleView,Location + 1);
  2876.     }
  2877.  
  2878.  
  2879. /* make sure the loop start and end are within the proper ranges */
  2880. void                                AdjustSampleWindowThangs(SampleWindowRec* Window)
  2881.     {
  2882.         long                            NumFrames;
  2883.  
  2884.         CheckPtrExistence(Window);
  2885.         NumFrames = GetSampleViewNumFrames(Window->SampleView);
  2886.         /* loop start check */
  2887.         if (SampleWindowGetLoopStart(Window) > NumFrames)
  2888.             {
  2889.                 SampleWindowPutLoopStart(Window,NumFrames);
  2890.             }
  2891.         if (SampleWindowGetLoopStart(Window) < 0)
  2892.             {
  2893.                 SampleWindowPutLoopStart(Window,0);
  2894.             }
  2895.         /* loop end check */
  2896.         if (SampleWindowGetLoopEnd(Window) > NumFrames)
  2897.             {
  2898.                 SampleWindowPutLoopEnd(Window,NumFrames);
  2899.             }
  2900.         if (SampleWindowGetLoopEnd(Window) < SampleWindowGetLoopStart(Window))
  2901.             {
  2902.                 SampleWindowPutLoopEnd(Window,SampleWindowGetLoopStart(Window));
  2903.             }
  2904.     }
  2905.  
  2906.  
  2907. /* recalculate the scroll bar displacements and redraw it */
  2908. void                                UpdateSampleWindowScrollbar(SampleWindowRec* Window)
  2909.     {
  2910.         CheckPtrExistence(Window);
  2911.         SetMaxScrollIndex(Window->Scrollbar,GetSampleViewNumFrames(Window->SampleView)
  2912.             - GetSampleViewNumVisibleFrames(Window->SampleView));
  2913.         SetScrollIndex(Window->Scrollbar,GetSampleViewXOffset(Window->SampleView));
  2914.         RedrawScrollBar(Window->Scrollbar);
  2915.     }
  2916.  
  2917.  
  2918. static void                    SampleWindowScrollHook(long Parameter, ScrollType How,
  2919.                                             SampleWindowRec* Window)
  2920.     {
  2921.         CheckPtrExistence(Window);
  2922.         switch (How)
  2923.             {
  2924.                 case eScrollToPosition:
  2925.                     SetSampleViewXOffset(Window->SampleView,Parameter);
  2926.                     UpdateSampleWindowScrollbar(Window);
  2927.                     SampleWindowRedrawThangs(Window);
  2928.                     break;
  2929.                 case eScrollPageMinus:
  2930.                     SetSampleViewXOffset(Window->SampleView,
  2931.                         GetSampleViewXOffset(Window->SampleView)
  2932.                         - ((7 * GetSampleViewNumVisibleFrames(Window->SampleView)) / 8));
  2933.                     UpdateSampleWindowScrollbar(Window);
  2934.                     SampleWindowRedrawThangs(Window);
  2935.                     break;
  2936.                 case eScrollPagePlus:
  2937.                     SetSampleViewXOffset(Window->SampleView,
  2938.                         GetSampleViewXOffset(Window->SampleView)
  2939.                         + ((7 * GetSampleViewNumVisibleFrames(Window->SampleView)) / 8));
  2940.                     UpdateSampleWindowScrollbar(Window);
  2941.                     SampleWindowRedrawThangs(Window);
  2942.                     break;
  2943.                 case eScrollLineMinus:
  2944.                     SetSampleViewXOffset(Window->SampleView,
  2945.                         GetSampleViewXOffset(Window->SampleView)
  2946.                         - ((1 * GetSampleViewNumVisibleFrames(Window->SampleView)) / 8));
  2947.                     UpdateSampleWindowScrollbar(Window);
  2948.                     SampleWindowRedrawThangs(Window);
  2949.                     break;
  2950.                 case eScrollLinePlus:
  2951.                     SetSampleViewXOffset(Window->SampleView,
  2952.                         GetSampleViewXOffset(Window->SampleView)
  2953.                         + ((1 * GetSampleViewNumVisibleFrames(Window->SampleView)) / 8));
  2954.                     UpdateSampleWindowScrollbar(Window);
  2955.                     SampleWindowRedrawThangs(Window);
  2956.                     break;
  2957.                 default:
  2958.                     EXECUTE(PRERR(AllowResume,"SampleWindowScrollHook:  Unknown scroll opcode"));
  2959.                     break;
  2960.             }
  2961.         SampleWindowPutMousePosition(Window);
  2962.     }
  2963.  
  2964.  
  2965. /* handle a mouse down in the sample edit area. */
  2966. void                                SampleWindowDoMouseDownInEdit(SampleWindowRec* Window, OrdType OrigX,
  2967.                                             OrdType OrigY)
  2968.     {
  2969.         long                            BaseSelectionStart;
  2970.         long                            BaseSelectionEnd;
  2971.         OrdType                        WhereX;
  2972.         OrdType                        WhereY;
  2973.  
  2974.         CheckPtrExistence(Window);
  2975.  
  2976.         /* first, check for a scrollbar operation */
  2977.         if (ScrollHitTest(Window->Scrollbar,OrigX,OrigY))
  2978.             {
  2979.                 ScrollHitProc(Window->Scrollbar,CheckModifiers(),OrigX,
  2980.                     OrigY,Window,(void (*)(long,ScrollType,void*))&SampleWindowScrollHook);
  2981.                 return;
  2982.             }
  2983.         /* now check for a parameter (origin, loop...) drag operation */
  2984.         if (OrigY < SAMPLEEDITY + (3 * OVERLINEHEIGHT))
  2985.             {
  2986.                 MyBoolean                DoingOrigin = False;
  2987.                 MyBoolean                DoingLoopStart = False;
  2988.                 MyBoolean                DoingLoopEnd = False;
  2989.  
  2990.                 if (OrigY < SAMPLEEDITY + OVERLINEHEIGHT)
  2991.                     {
  2992.                         DoingOrigin = True;
  2993.                     }
  2994.                 else if (OrigY < SAMPLEEDITY + (2 * OVERLINEHEIGHT))
  2995.                     {
  2996.                         DoingLoopStart = True;
  2997.                     }
  2998.                 else
  2999.                     {
  3000.                         DoingLoopEnd = True;
  3001.                     }
  3002.                 while (eMouseUp != GetAnEvent(&WhereX,&WhereY,NIL,NIL,NIL,NIL))
  3003.                     {
  3004.                         long                CurrentMouseX;
  3005.  
  3006.                         SampleWindowPutMousePosition(Window);
  3007.                         WhereX -= (GetSampleViewXLoc(Window->SampleView) + 1);
  3008.                         if (WhereX < 0)
  3009.                             {
  3010.                                 /* drag left */
  3011.                                 SampleWindowScrollHook(0,eScrollLineMinus,Window);
  3012.                             }
  3013.                         if (WhereX > SAMPLEEDITWIDTH(GetWindowWidth(Window->ScreenID)))
  3014.                             {
  3015.                                 /* drag right */
  3016.                                 SampleWindowScrollHook(0,eScrollLinePlus,Window);
  3017.                             }
  3018.                         CurrentMouseX = WhereX * GetSampleViewHorizontalScale(Window->SampleView)
  3019.                             + GetSampleViewXOffset(Window->SampleView);
  3020.                         /* perform modification & redraw */
  3021.                         SampleWindowRedrawUnderThangs(Window);
  3022.                         if (DoingOrigin)
  3023.                             {
  3024.                                 SampleWindowPutOrigin(Window,CurrentMouseX);
  3025.                             }
  3026.                         else if (DoingLoopStart)
  3027.                             {
  3028.                                 SampleWindowPutLoopStart(Window,CurrentMouseX);
  3029.                             }
  3030.                         else /* if (DoingLoopEnd) */
  3031.                             {
  3032.                                 SampleWindowPutLoopEnd(Window,CurrentMouseX);
  3033.                             }
  3034.                         AdjustSampleWindowThangs(Window);
  3035.                         SampleWindowRedrawThangs(Window);
  3036.                     }
  3037.                 SampleWindowPutMousePosition(Window);
  3038.                 return;
  3039.             }
  3040.  
  3041.         /* convert mouse location into local coordinates */
  3042.         WhereX = OrigX - (GetSampleViewXLoc(Window->SampleView) + 1);
  3043.  
  3044.         /* set up base selection */
  3045.         if ((CheckModifiers() & eShiftKey) != 0)
  3046.             {
  3047.                 /* extending existing selection */
  3048.                 BaseSelectionStart = GetSampleViewSelectStart(Window->SampleView);
  3049.                 BaseSelectionEnd = GetSampleViewSelectEnd(Window->SampleView);
  3050.             }
  3051.          else
  3052.             {
  3053.                 /* replacing existing selection */
  3054.                 BaseSelectionStart = WhereX * GetSampleViewHorizontalScale(Window->SampleView)
  3055.                     + GetSampleViewXOffset(Window->SampleView);
  3056.                 BaseSelectionEnd = BaseSelectionStart;
  3057.                 SetSampleViewSelection(Window->SampleView,BaseSelectionStart,BaseSelectionEnd);
  3058.             }
  3059.  
  3060.         /* track mouse */
  3061.         while (eMouseUp != GetAnEvent(&WhereX,&WhereY,NIL,NIL,NIL,NIL))
  3062.             {
  3063.                 long                CurrentMouseX;
  3064.                 long                NewTempSelectStart;
  3065.                 long                NewTempSelectEnd;
  3066.  
  3067.                 SampleWindowPutMousePosition(Window);
  3068.                 SampleWindowPutSelectStart(Window);
  3069.                 SampleWindowPutSelectEnd(Window);
  3070.                 WhereX -= (GetSampleViewXLoc(Window->SampleView) + 1);
  3071.                 if (WhereX < 0)
  3072.                     {
  3073.                         /* drag left */
  3074.                         SampleWindowScrollHook(0,eScrollLineMinus,Window);
  3075.                     }
  3076.                 if (WhereX > SAMPLEEDITWIDTH(GetWindowWidth(Window->ScreenID)))
  3077.                     {
  3078.                         /* drag right */
  3079.                         SampleWindowScrollHook(0,eScrollLinePlus,Window);
  3080.                     }
  3081.                 /* find union of selection areas */
  3082.                 CurrentMouseX = WhereX * GetSampleViewHorizontalScale(Window->SampleView)
  3083.                     + GetSampleViewXOffset(Window->SampleView);
  3084.                 NewTempSelectStart = BaseSelectionStart;
  3085.                 NewTempSelectEnd = BaseSelectionEnd;
  3086.                 if (CurrentMouseX < NewTempSelectStart)
  3087.                     {
  3088.                         NewTempSelectStart = CurrentMouseX;
  3089.                     }
  3090.                 if (CurrentMouseX > NewTempSelectEnd)
  3091.                     {
  3092.                         NewTempSelectEnd = CurrentMouseX;
  3093.                     }
  3094.                 /* set new selection region */
  3095.                 SetSampleViewSelection(Window->SampleView,NewTempSelectStart,NewTempSelectEnd);
  3096.             }
  3097.         /* now we leave, and the selection has been adjusted as desired */
  3098.         SampleWindowPutMousePosition(Window);
  3099.         SampleWindowPutSelectStart(Window);
  3100.         SampleWindowPutSelectEnd(Window);
  3101.         SampleWindowRedrawThangs(Window);
  3102.     }
  3103.  
  3104.  
  3105. /* find out which loop the sample is editing */
  3106. SampleLoops                    SampleWindowEditingWhichLoop(SampleWindowRec* Window)
  3107.     {
  3108.         CheckPtrExistence(Window);
  3109.         return Window->EditingWhichLoop;
  3110.     }
  3111.  
  3112.  
  3113. /* force the sample data to be written back to the object.  this does not write */
  3114. /* back any other data. */
  3115. MyBoolean                        SampleWindowForceUpdateSampleObjectData(SampleWindowRec* Window)
  3116.     {
  3117.         SampleStorageActualRec*        TheStuff;
  3118.  
  3119.         TheStuff = SampleWindowGetTheSampleActual(Window);
  3120.         if (TheStuff != NIL)
  3121.             {
  3122.                 /* this call disposes the sample for us */
  3123.                 SampleObjectPutNewSample(Window->SampleObject,TheStuff);
  3124.                 SampleViewHasBeenSaved(Window->SampleView);
  3125.                 return True;
  3126.             }
  3127.          else
  3128.             {
  3129.                 return False;
  3130.             }
  3131.     }
  3132.  
  3133.  
  3134. /* get the raw data for the sample, in char or short form.  the caller must NOT */
  3135. /* dispose of this data, and it will become invalid if any modifications are made */
  3136. /* to the sample.  This is basically SampleObjectGetRawData() with a call to */
  3137. /* SampleWindowForceUpdateSampleObjectData() to keep everything consistent. */
  3138. char*                                SampleWindowGetRawData(SampleWindowRec* Window)
  3139.     {
  3140.         /* this is a weird routine:  it forces the sample object to be up to date, then */
  3141.         /* returns the sample object's idea of what the sample is.  Be careful because */
  3142.         /* it can lead to recursive infinite loops */
  3143.         CheckPtrExistence(Window);
  3144.         if (!SampleWindowForceUpdateSampleObjectData(Window))
  3145.             {
  3146.                 /* failed */
  3147.             }
  3148.         return SampleObjectGetRawData(Window->SampleObject);
  3149.     }
  3150.  
  3151.  
  3152. /* get the raw fixedpoint data for the sample. */
  3153. largefixedsigned*        SampleWindowGetRawFixedPointData(SampleWindowRec* Window)
  3154.     {
  3155.         CheckPtrExistence(Window);
  3156.         return SampleViewGetActualRawData(Window->SampleView);
  3157.     }
  3158.  
  3159.  
  3160. /* the document title has changed, so change the window title bar.  caller is */
  3161. /* responsible for deleting the string, which should be non-null-terminated. */
  3162. void                                SampleWindowGlobalNameChange(SampleWindowRec* Window,
  3163.                                             char* NewFilename)
  3164.     {
  3165.         char*                            LocalNameCopy;
  3166.  
  3167.         CheckPtrExistence(Window);
  3168.         CheckPtrExistence(NewFilename);
  3169.         LocalNameCopy = TextEditGetRawData(Window->NameEdit,SYSTEMLINEFEED);
  3170.         if (LocalNameCopy != NIL)
  3171.             {
  3172.                 char*                            SeparatorString;
  3173.  
  3174.                 SeparatorString = StringToBlockCopy(":  ");
  3175.                 if (SeparatorString != NIL)
  3176.                     {
  3177.                         char*                            LeftHalfOfString;
  3178.  
  3179.                         LeftHalfOfString = ConcatBlockCopy(NewFilename,SeparatorString);
  3180.                         if (LeftHalfOfString != NIL)
  3181.                             {
  3182.                                 char*                            TotalString;
  3183.  
  3184.                                 TotalString = ConcatBlockCopy(LeftHalfOfString,LocalNameCopy);
  3185.                                 if (TotalString != NIL)
  3186.                                     {
  3187.                                         char*                            NullTerminatedString;
  3188.  
  3189.                                         NullTerminatedString = BlockToStringCopy(TotalString);
  3190.                                         if (NullTerminatedString != NIL)
  3191.                                             {
  3192.                                                 SetWindowName(Window->ScreenID,NullTerminatedString);
  3193.                                                 ChangeItemName(Window->MyMenuItem,NullTerminatedString);
  3194.                                                 ReleasePtr(NullTerminatedString);
  3195.                                             }
  3196.                                         ReleasePtr(TotalString);
  3197.                                     }
  3198.                                 ReleasePtr(LeftHalfOfString);
  3199.                             }
  3200.                         ReleasePtr(SeparatorString);
  3201.                     }
  3202.                 ReleasePtr(LocalNameCopy);
  3203.             }
  3204.     }
  3205.  
  3206.  
  3207. /* force the window title bar to be renamed even if the document hasn't been renamed */
  3208. void                                SampleWindowResetTitlebar(SampleWindowRec* Window)
  3209.     {
  3210.         char*                            DocumentName;
  3211.  
  3212.         CheckPtrExistence(Window);
  3213.         DocumentName = GetCopyOfDocumentName(Window->MainWindow);
  3214.         if (DocumentName != NIL)
  3215.             {
  3216.                 SampleWindowGlobalNameChange(Window,DocumentName);
  3217.                 ReleasePtr(DocumentName);
  3218.             }
  3219.     }
  3220.  
  3221.  
  3222. /* get an 'actual' sample object that contains the same data as the window.  the */
  3223. /* caller must delete the object. */
  3224. struct SampleStorageActualRec*    SampleWindowGetTheSampleActual(SampleWindowRec* Window)
  3225.     {
  3226.         SampleStorageActualRec*        TheStuff;
  3227.         NumChannelsType                        NumChannels;
  3228.         long                                            Limit;
  3229.         long                                            Scan;
  3230.  
  3231.         NumChannels = SampleWindowGetNumChannels(Window);
  3232.         Limit = GetSampleViewNumFrames(Window->SampleView);
  3233.         TheStuff = NewSampleStorageActual(SampleWindowGetNumBits(Window),
  3234.             NumChannels,Limit);
  3235.         if (TheStuff != NIL)
  3236.             {
  3237.                 switch (NumChannels)
  3238.                     {
  3239.                         default:
  3240.                             EXECUTE(PRERR(ForceAbort,"SampleWindowGetTheSampleActual:  bad num bits"));
  3241.                             break;
  3242.                         case eSampleMono:
  3243.                             for (Scan = 0; Scan < Limit; Scan += 1)
  3244.                                 {
  3245.                                     SetSampleStorageActualValue(TheStuff,Scan,eMonoChannel,
  3246.                                         GetSampleViewValue(Window->SampleView,Scan,eMonoChannel));
  3247.                                 }
  3248.                             break;
  3249.                         case eSampleStereo:
  3250.                             for (Scan = 0; Scan < Limit; Scan += 1)
  3251.                                 {
  3252.                                     SetSampleStorageActualValue(TheStuff,Scan,eLeftChannel,
  3253.                                         GetSampleViewValue(Window->SampleView,Scan,eLeftChannel));
  3254.                                     SetSampleStorageActualValue(TheStuff,Scan,eRightChannel,
  3255.                                         GetSampleViewValue(Window->SampleView,Scan,eRightChannel));
  3256.                                 }
  3257.                             break;
  3258.                     }
  3259.             }
  3260.         return TheStuff;
  3261.     }
  3262.  
  3263.  
  3264. /* force ALL modified data to be written back to the object.  this includes */
  3265. /* calling SampleWindowForceUpdateSampleObjectData() */
  3266. MyBoolean                        SampleWindowWritebackModifiedData(SampleWindowRec* Window)
  3267.     {
  3268.         MyBoolean                    SuccessFlag = True;
  3269.  
  3270.         CheckPtrExistence(Window);
  3271.  
  3272.         /* if an edit is being edited, then finalize it */
  3273.         SampleWindowFinalizeCurrentEdit(Window);
  3274.  
  3275.         if (TextEditDoesItNeedToBeSaved(Window->NameEdit))
  3276.             {
  3277.                 char*                            TempString;
  3278.  
  3279.                 TempString = SampleWindowGetNameCopy(Window);
  3280.                 if (TempString != NIL)
  3281.                     {
  3282.                         SampleObjectNewName(Window->SampleObject,TempString);
  3283.                         TextEditHasBeenSaved(Window->NameEdit);
  3284.                     }
  3285.                  else
  3286.                     {
  3287.                         SuccessFlag = False;
  3288.                     }
  3289.             }
  3290.  
  3291.         if (TextEditDoesItNeedToBeSaved(Window->OriginEdit))
  3292.             {
  3293.                 SampleObjectPutOrigin(Window->SampleObject,SampleWindowGetOrigin(Window));
  3294.                 TextEditHasBeenSaved(Window->OriginEdit);
  3295.             }
  3296.  
  3297.         if (TextEditDoesItNeedToBeSaved(Window->LoopStartEdit))
  3298.             {
  3299.                 switch (Window->EditingWhichLoop)
  3300.                     {
  3301.                         default:
  3302.                             EXECUTE(PRERR(ForceAbort,
  3303.                                 "DisposeSampleWindow:  bad value in EditingWhichLoop"));
  3304.                             break;
  3305.                         case eSampleLoop1:
  3306.                             SampleObjectPutLoopStart1(Window->SampleObject,
  3307.                                 SampleWindowGetLoopStart(Window));
  3308.                             break;
  3309.                         case eSampleLoop2:
  3310.                             SampleObjectPutLoopStart2(Window->SampleObject,
  3311.                                 SampleWindowGetLoopStart(Window));
  3312.                             break;
  3313.                         case eSampleLoop3:
  3314.                             SampleObjectPutLoopStart3(Window->SampleObject,
  3315.                                 SampleWindowGetLoopStart(Window));
  3316.                             break;
  3317.                     }
  3318.                 TextEditHasBeenSaved(Window->LoopStartEdit);
  3319.             }
  3320.  
  3321.         if (TextEditDoesItNeedToBeSaved(Window->LoopEndEdit))
  3322.             {
  3323.                 switch (Window->EditingWhichLoop)
  3324.                     {
  3325.                         default:
  3326.                             EXECUTE(PRERR(ForceAbort,
  3327.                                 "DisposeSampleWindow:  bad value in EditingWhichLoop"));
  3328.                             break;
  3329.                         case eSampleLoop1:
  3330.                             SampleObjectPutLoopEnd1(Window->SampleObject,
  3331.                                 SampleWindowGetLoopEnd(Window));
  3332.                             break;
  3333.                         case eSampleLoop2:
  3334.                             SampleObjectPutLoopEnd2(Window->SampleObject,
  3335.                                 SampleWindowGetLoopEnd(Window));
  3336.                             break;
  3337.                         case eSampleLoop3:
  3338.                             SampleObjectPutLoopEnd3(Window->SampleObject,
  3339.                                 SampleWindowGetLoopEnd(Window));
  3340.                             break;
  3341.                     }
  3342.                 TextEditHasBeenSaved(Window->LoopEndEdit);
  3343.             }
  3344.  
  3345.         if (TextEditDoesItNeedToBeSaved(Window->SamplingRateEdit))
  3346.             {
  3347.                 SampleObjectPutSamplingRate(Window->SampleObject,
  3348.                     SampleWindowGetSamplingRate(Window));
  3349.                 TextEditHasBeenSaved(Window->SamplingRateEdit);
  3350.             }
  3351.  
  3352.         if (TextEditDoesItNeedToBeSaved(Window->NaturalFrequencyEdit))
  3353.             {
  3354.                 SampleObjectPutNaturalFrequency(Window->SampleObject,
  3355.                     SampleWindowGetNaturalFrequency(Window));
  3356.                 TextEditHasBeenSaved(Window->NaturalFrequencyEdit);
  3357.             }
  3358.  
  3359.         if (TextEditDoesItNeedToBeSaved(Window->SampleExpressionEdit))
  3360.             {
  3361.                 char*                        TextTemp;
  3362.  
  3363.                 TextTemp = SampleWindowGetFormulaCopy(Window);
  3364.                 if (TextTemp != NIL)
  3365.                     {
  3366.                         SampleObjectNewFormula(Window->SampleObject,TextTemp);
  3367.                         TextEditHasBeenSaved(Window->SampleExpressionEdit);
  3368.                     }
  3369.                  else
  3370.                     {
  3371.                         SuccessFlag = False;
  3372.                     }
  3373.             }
  3374.  
  3375.         if (!SampleWindowForceUpdateSampleObjectData(Window))
  3376.             {
  3377.                 SuccessFlag = False;
  3378.             }
  3379.  
  3380.         return SuccessFlag;
  3381.     }
  3382.  
  3383.  
  3384. /* this routine updates the loop points when some number of sample frames are */
  3385. /* inserted or removed from the specified point.  (for removal, NumAddedFrames < 0) */
  3386. /* it does NOT do any redrawing of the points. */
  3387. void                                SampleWindowShiftPoints(SampleWindowRec* Window, long Position,
  3388.                                             long NumAddedFrames)
  3389.     {
  3390.         long                            Temp;
  3391.  
  3392.         CheckPtrExistence(Window);
  3393.         SampleWindowFinalizeCurrentEdit(Window);
  3394.  
  3395.         Temp = SampleObjectGetOrigin(Window->SampleObject);
  3396.         if (Temp >= Position)
  3397.             {
  3398.                 Temp += NumAddedFrames;
  3399.                 if (Temp < Position)
  3400.                     {
  3401.                         Temp = Position;
  3402.                     }
  3403.                 SampleObjectPutOrigin(Window->SampleObject,Temp);
  3404.                 SampleWindowPutOrigin(Window,Temp);
  3405.             }
  3406.  
  3407.         Temp = SampleObjectGetLoopStart1(Window->SampleObject);
  3408.         if (Temp >= Position)
  3409.             {
  3410.                 Temp += NumAddedFrames;
  3411.                 if (Temp < Position)
  3412.                     {
  3413.                         Temp = Position;
  3414.                     }
  3415.                 SampleObjectPutLoopStart1(Window->SampleObject,Temp);
  3416.                 if (Window->EditingWhichLoop == eSampleLoop1)
  3417.                     {
  3418.                         SampleWindowPutLoopStart(Window,Temp);
  3419.                     }
  3420.             }
  3421.  
  3422.         Temp = SampleObjectGetLoopStart2(Window->SampleObject);
  3423.         if (Temp >= Position)
  3424.             {
  3425.                 Temp += NumAddedFrames;
  3426.                 if (Temp < Position)
  3427.                     {
  3428.                         Temp = Position;
  3429.                     }
  3430.                 SampleObjectPutLoopStart2(Window->SampleObject,Temp);
  3431.                 if (Window->EditingWhichLoop == eSampleLoop2)
  3432.                     {
  3433.                         SampleWindowPutLoopStart(Window,Temp);
  3434.                     }
  3435.             }
  3436.  
  3437.         Temp = SampleObjectGetLoopStart3(Window->SampleObject);
  3438.         if (Temp >= Position)
  3439.             {
  3440.                 Temp += NumAddedFrames;
  3441.                 if (Temp < Position)
  3442.                     {
  3443.                         Temp = Position;
  3444.                     }
  3445.                 SampleObjectPutLoopStart3(Window->SampleObject,Temp);
  3446.                 if (Window->EditingWhichLoop == eSampleLoop3)
  3447.                     {
  3448.                         SampleWindowPutLoopStart(Window,Temp);
  3449.                     }
  3450.             }
  3451.  
  3452.         Temp = SampleObjectGetLoopEnd1(Window->SampleObject);
  3453.         if (Temp >= Position)
  3454.             {
  3455.                 Temp += NumAddedFrames;
  3456.                 if (Temp < Position)
  3457.                     {
  3458.                         Temp = Position;
  3459.                     }
  3460.                 SampleObjectPutLoopEnd1(Window->SampleObject,Temp);
  3461.                 if (Window->EditingWhichLoop == eSampleLoop1)
  3462.                     {
  3463.                         SampleWindowPutLoopEnd(Window,Temp);
  3464.                     }
  3465.             }
  3466.  
  3467.         Temp = SampleObjectGetLoopEnd2(Window->SampleObject);
  3468.         if (Temp >= Position)
  3469.             {
  3470.                 Temp += NumAddedFrames;
  3471.                 if (Temp < Position)
  3472.                     {
  3473.                         Temp = Position;
  3474.                     }
  3475.                 SampleObjectPutLoopEnd2(Window->SampleObject,Temp);
  3476.                 if (Window->EditingWhichLoop == eSampleLoop2)
  3477.                     {
  3478.                         SampleWindowPutLoopEnd(Window,Temp);
  3479.                     }
  3480.             }
  3481.  
  3482.         Temp = SampleObjectGetLoopEnd3(Window->SampleObject);
  3483.         if (Temp >= Position)
  3484.             {
  3485.                 Temp += NumAddedFrames;
  3486.                 if (Temp < Position)
  3487.                     {
  3488.                         Temp = Position;
  3489.                     }
  3490.                 SampleObjectPutLoopEnd3(Window->SampleObject,Temp);
  3491.                 if (Window->EditingWhichLoop == eSampleLoop3)
  3492.                     {
  3493.                         SampleWindowPutLoopEnd(Window,Temp);
  3494.                     }
  3495.             }
  3496.     }
  3497.